andrew4582

SetTitleValue [Winform]

Dec 13th, 2011
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1.         /// <summary>
  2.         /// Allows an update to a window's title with added a new value while maintaining the base title.
  3.         /// 'Notepad'
  4.         /// </summary>
  5.         /// <remarks>
  6.         /// Example:
  7.         /// string result = SetTitleValue("Notepad","Document1");
  8.         /// MessageBox.Show(result);
  9.         /// // result = 'Notepad - Document1'
  10.         /// result = SetTitleValue("Notepad",null); // or string.Empty
  11.         /// // result = 'Notepad';
  12.         /// </remarks>
  13.         /// <param name="titleBase">The base start of the title</param>
  14.         /// <param name="value">The value appended onto the <see cref="titleBase"/></param>
  15.         /// <returns>
  16.         /// The formated title as "{0}{1}{2}", where
  17.         ///     {0} is <see cref="titleBase"/>,
  18.         ///     {1} is <see cref="separator"/>, and
  19.         ///     {2} is <see cref="value"/>
  20.         /// </returns>
  21.         public static string SetTitleValue(string titleBase, string value,string separator = " - ") {
  22.            
  23.             string s = value;
  24.  
  25.             if(string.IsNullOrWhiteSpace(s))
  26.                 s = string.Empty;
  27.  
  28.             string title = s
  29.                 .Trim()
  30.                 .TrimEnd(separator.ToCharArray());
  31.  
  32.             if(title.Equals(titleBase,StringComparison.InvariantCultureIgnoreCase)) {
  33.                 title = string.Empty;
  34.             }
  35.  
  36.             if(string.IsNullOrWhiteSpace(title))
  37.                 separator = string.Empty;
  38.  
  39.             return string.Format("{0}{1}{2}",titleBase,separator,title);
  40.         }
Advertisement
Add Comment
Please, Sign In to add comment