My dll has some real-time messages which are printed out using:
Console.Writeline("XYZ Message");
I have referenced this dll in my C# Windows form application. The messages are then printed out in real-time using:
public MyClientMainForm()
{
AllocConsole();
InitializeComponent();
}
But using this way I am launching a separate console window for displaying the messages.
Instead of a separate Console window, I wish to re-direct these messages to a Listbox
inside my Winform.
Can someone help me with a simple example for this.
重定向Console.WriteLine到String也许是这样,您可以使用listBox1.Items.Add(stringName);
将输出转换为String,然后从String转换为ListBox listBox1.Items.Add(stringName);
Another, probably cleaner way to do this is to extend TextWriter with your own that logs to wherever you'd like it to.
Note: I have not tested this.
public class ListBoxWriter : TextWriter
{
private ListBox list;
private StringBuilder content = new StringBuilder();
public ListBoxWriter(ListBox list)
{
this.list = list;
}
public override void Write(char value)
{
base.Write(value);
content.Append(value);
if (value == '\n')
{
list.Items.Add(content.ToString());
content = new StringBuilder();
}
}
public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.