using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Windows.Forms; namespace DomenicDenicola { /// /// A collection of extension methods used by Domenic Denicola. /// public static class Extensions { /// /// Formats a given time span in the form H:mm. /// /// The to be formatted. /// A containing in the desired H:mm format. public static string ToHourMinuteString(this TimeSpan span) { return string.Format(CultureInfo.InvariantCulture, "{0:0}:{1:00}", (int)span.TotalMinutes / 60, Math.Abs(span.TotalMinutes % 60)); } /// /// Formats a given color as a hexadecimal string, of the form "RRGGBB." /// /// The to be formatted. /// A containing in the desired RRGGBB format. public static string ToHexString(this Color color) { return string.Format(CultureInfo.InvariantCulture, "{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B); } /// /// Adjusts a color's red, green, and blue values by a given amount. /// /// The color to adjust. /// The amount by which to adjust each component of the color. /// A resulting from adding to the red, green, and blue components of , with values above 255 or below 0 appropriately bounded to within that range. public static Color Adjust(this Color color, int adjustment) { Func adjust = input => Math.Max(Math.Min(input + adjustment, 255), 0); return Color.FromArgb(color.A, adjust(color.R), adjust(color.G), adjust(color.B)); } /// /// Returns a string array that contains the substrings in this string that are delimited by the specified Unicode character. /// A parameter specifies whether to return empty array elements. /// /// The to split. /// A Unicode character that delimits the substrings in this string. /// to omit empty array elements from the array returned; /// or to include empty array elements in the array returned. /// An array whose elements contain the substrings in this string that are delimited by . public static string[] Split(this string theString, char separator, StringSplitOptions options) { return theString.Split(new char[] { separator }, options); } /// /// Returns a string array that contains the substrings in this string that are delimited by the specified string. /// A parameter specifies whether to return empty array elements. /// /// The to split. /// A string that delimits the substrings in this string. /// to omit empty array elements from the array returned; /// or to include empty array elements in the array returned. /// An array whose elements contain the substrings in this string that are delimited by . public static string[] Split(this string theString, string separator, StringSplitOptions options) { return theString.Split(new string[] { separator }, options); } /// /// Formats a number as a string according to the current culture format info. /// /// The number to be formatted. /// A string containing the value of , according to . /// Spaces in the returned string are replaced with non-breaking spaces (U+00A0), for easier display purposes. public static string MakeFormattedString(this int number) { return number.ToString("#,0", NumberFormatInfo.CurrentInfo).Replace(' ', '\u00A0'); } /// /// Creates a human-readable list from the given enumerable collection. /// /// An array of strings denoting the items to appear in the list. /// A comma-delimited list of items, following English grammar rules wherein a two-item list has no commas but does have an “and,” and lists with more items have an “and” after the last comma. /// is . public static string MakeHumanReadableList(this IEnumerable listItems) { string itemsString = string.Join(", ", listItems.ToArray()); return listItems.Count() == 2 ? itemsString.Replace(", ", " and ") : itemsString.Insert(itemsString.LastIndexOf(',') + 1, " and"); } // These are to make your life easier when using Invoke and BeginInvoke on forms/controls; // without them the syntax is rather cumbersome, e.g. lambdas are not allowed. #region Invoke/BeginInvoke methods using delegates public static TResult Invoke(this Control me, Func invoker, T arg) { if (me.InvokeRequired) { return (TResult)me.Invoke(invoker, arg); } else { return invoker(arg); } } public static TResult Invoke(this Control me, Func invoker) { if (me.InvokeRequired) { return (TResult)me.Invoke(invoker); } else { return invoker(); } } public static void Invoke(this Control me, Action invoker, T arg) { if (me.InvokeRequired) { me.Invoke(invoker, arg); } else { invoker(arg); } } public static void Invoke(this Control me, Action invoker, params object[] args) { if (me.InvokeRequired) { me.Invoke(invoker, args); } else { invoker.DynamicInvoke(args); } } public static void Invoke(this Control me, Action invoker) { if (me.InvokeRequired) { me.Invoke(invoker); } else { invoker(); } } public static TResult BeginInvoke(this Control me, Func invoker, T arg) { if (me.InvokeRequired) { return (TResult)me.BeginInvoke(invoker, arg); } else { return invoker(arg); } } public static TResult BeginInvoke(this Control me, Func invoker) { if (me.InvokeRequired) { return (TResult)me.BeginInvoke(invoker); } else { return invoker(); } } public static void BeginInvoke(this Control me, Action invoker, T arg) { if (me.InvokeRequired) { me.BeginInvoke(invoker, arg); } else { invoker(arg); } } public static void BeginInvoke(this Control me, Action invoker, params object[] args) { if (me.InvokeRequired) { me.BeginInvoke(invoker, args); } else { invoker.DynamicInvoke(args); } } public static void BeginInvoke(this Control me, Action invoker) { if (me.InvokeRequired) { me.BeginInvoke(invoker); } else { invoker(); } } #endregion #region MessageBox methods public static DialogResult MessageBox(this Form form, string text, string caption, MessageBoxButtons buttons, MessageBoxIcon icon) { Func messageBox = delegate { MessageBoxOptions options = (MessageBoxOptions)0; if (form.RightToLeft == RightToLeft.Yes) { options |= MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading; } return System.Windows.Forms.MessageBox.Show(form, text, caption, buttons, icon, MessageBoxDefaultButton.Button1, options); }; if (form.InvokeRequired) { return (DialogResult)form.Invoke(messageBox); } return messageBox(); } public static void MessageBox(this Form form, string text, string caption, MessageBoxIcon icon) { form.MessageBox(text, caption, MessageBoxButtons.OK, icon); } public static void ErrorBox(this Form form, string text, string caption) { form.MessageBox(text, caption, MessageBoxButtons.OK, MessageBoxIcon.Error); } #endregion } }