C# WinForm 中Console 重定向輸出到ListBox控件中顯示


 1  public  class TextBoxWriter : System.IO.TextWriter
 2     {
 3         ListBox lstBox;
 4          delegate  void VoidAction();
 5 
 6          public TextBoxWriter(ListBox box)
 7         {
 8             lstBox = box;
 9         }
10 
11          public  override  void Write( string value)
12         {
13             VoidAction action =  delegate
14             {
15                 lstBox.Items.Insert( 0string.Format( " [{0:HH:mm:ss}]{1} ", DateTime.Now, value));
16             };
17             lstBox.BeginInvoke(action);
18         }
19 
20          public  override  void WriteLine( string value)
21         {
22             VoidAction action =  delegate
23             {
24                 lstBox.Items.Insert( 0string.Format( " [{0:HH:mm:ss}]{1} ", DateTime.Now, value));
25             };
26             lstBox.BeginInvoke(action);
27         }
28 
29          public  override System.Text.Encoding Encoding
30         {
31              get {  return System.Text.Encoding.UTF8; }
32         }
33     }    

 

   void frmMain_Load( object sender, EventArgs e)
        {
            Console.SetOut( new TextBoxWriter(Instances.ucLog.LogBox));
        }

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM