andrew4582

WriteOutput code part

Dec 16th, 2011
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. /// <summary>
  2.         ///  Writes output to a windows form using InvokeRequired property
  3.         /// </summary>
  4.         /// <param name="formatOrValue">Value to write or format used in formating strings</param>
  5.         /// <param name="args">Parameters used in formatting strings</param>
  6.         void WriteOutput(object formatOrValue,params object[] args) {
  7.  
  8.             string text = string.Empty;
  9.  
  10.             if(args == null || args.Length == 0) {
  11.                 if(formatOrValue != null)
  12.                     text = formatOrValue.ToString();
  13.                 else
  14.                     text = string.Empty;
  15.             }
  16.             else {
  17.                 text = string.Format(formatOrValue.ToString(),args);
  18.             }
  19.  
  20.             text += Environment.NewLine;
  21.  
  22.  
  23.             Action<string> writeAction = (s) => {
  24.                 richTextBox1.AppendText(s);
  25.                 ScrollOutputToCaret();
  26.             };
  27.  
  28.             if(InvokeRequired)
  29.                 BeginInvoke(new Action<string>(writeAction),text);
  30.             else
  31.                 writeAction(text);
  32.         }
  33.  
  34.         void ScrollOutputToCaret() {
  35.             if(richTextBox1.TextLength == 0)
  36.                 return;
  37.             richTextBox1.Select(richTextBox1.TextLength,1);
  38.             richTextBox1.ScrollToCaret();
  39.         }
  40.  
  41.         void ClearOutput() {
  42.  
  43.             Action actionInvoke = () => {
  44.                 richTextBox1.Clear();
  45.             };
  46.  
  47.             if(InvokeRequired)
  48.                 Invoke(actionInvoke);
  49.             else
  50.                 actionInvoke();
  51.         }
Advertisement
Add Comment
Please, Sign In to add comment