我想修改Console.WriteLine重定向方法。下面是我正在使用的重定向代码(完美地工作)但是//修改后的注释之间添加了不能满足我需要的代码。 我想将Console.writeline(字符串)中的字符串也复制到另一个列表框,但列表框会更改。 FE,现在我这样做:我写Console.WriteLine(“sometext”+“** i”);我在哪里显示字符串也应该写入。 当前代码无效。在其他列表框中没有任何内容出现在应该复制的位置,并且行在默认列表框中被双重写入。 在之前的版本中,我没有使用'override WriteLine'和'½'标记。相反,我检查'\ 0'以显示字符串的结尾(这在下面的代码中注释掉)。这并没有给出默认列表框中的坏双线并在另一个列表框中写了一些东西。问题是,对于多行字符串,它只将最后一行添加到另一个列表框中(但是将整个行添加到默认列表框中)。
有更好的方法吗? 请帮忙
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System;
using System.IO;
using System.Text;
using System.Windows.Forms;
namespace SefelecTester
{
public class ListBoxWriter : TextWriter//this class redirects console.writeline to debug listbox
{
private delegate void SetTextCallback(string text);
//TextWriter _writer = null;//for TextBoxWriter
private ListBox list;
//modified
private ListBox list2;
private ListBox list3;
private int select=0;
//--modified
private StringBuilder content = new StringBuilder();
public ListBoxWriter(ListBox list, ListBox list2, ListBox list3)
{
this.list = list;
//modified
this.list2 = list2;
this.list3 = list3;
//--modified
}
private void SetText(string text)
{
//this.rtbIncoming.Text += text;
Console.WriteLine(text);
}
public override void Write(char value)
{
base.Write(value);
content.Append(value);
//modified
if (value == '½')
//if (value == '\0')//previous version
{
this.list.Invoke(new MethodInvoker(() => list.Items.Add(content.ToString())));
select = 0;
return;//not in previous version
}
if (select!=1 ||select!=2)
{
if (select == 10)
{
this.list.Invoke(new MethodInvoker(() => list.Items.Add(content.ToString())));
select = (int)Char.GetNumericValue(value);
}
else if (value == '*' && select == 5)
{
this.list.Invoke(new MethodInvoker(() => list.Items.Add(content.ToString())));
select = 10;
}
else if (value == '*')
{
this.list.Invoke(new MethodInvoker(() => list.Items.Add(content.ToString())));
select = 5;
}
}
//--modified
if (value == '\n')
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.list.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
//this.Invoke(d, new object[] { content.ToString() });
try
{
this.list.Invoke(new MethodInvoker(() => list.Items.Add(content.ToString())));
this.list.Invoke(new MethodInvoker(() => list.SelectedIndex = list.Items.Count - 1));
this.list.Invoke(new MethodInvoker(() => list.SelectedIndex = -1));
}
catch (ObjectDisposedException ex)
{
Console.WriteLine("Exception raised(" + ex.Message + "): " + ex);
}
}
else
{
list.Items.Add(content.ToString());
list.SelectedIndex = list.Items.Count - 1;
list.SelectedIndex = -1;
}
//modified
if(select>0)
{
if(select==1)
{
if (this.list2.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
//this.Invoke(d, new object[] { content.ToString() });
try
{
this.list2.Invoke(new MethodInvoker(() => list2.Items.Add(content.ToString())));
this.list2.Invoke(new MethodInvoker(() => list2.SelectedIndex = list2.Items.Count - 1));
this.list2.Invoke(new MethodInvoker(() => list2.SelectedIndex = -1));
}
catch (ObjectDisposedException ex)
{
Console.WriteLine("Exception raised(" + ex.Message + "): " + ex);
}
}
else
{
list2.Items.Add(content.ToString());
list2.SelectedIndex = list2.Items.Count - 1;
list2.SelectedIndex = -1;
}
}
else if(select==2)
{
if (this.list.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
//this.Invoke(d, new object[] { content.ToString() });
try
{
this.list3.Invoke(new MethodInvoker(() => list3.Items.Add(content.ToString())));
this.list3.Invoke(new MethodInvoker(() => list3.SelectedIndex = list3.Items.Count - 1));
this.list3.Invoke(new MethodInvoker(() => list3.SelectedIndex = -1));
}
catch (ObjectDisposedException ex)
{
Console.WriteLine("Exception raised(" + ex.Message + "): " + ex);
}
}
else
{
list3.Items.Add(content.ToString());
list3.SelectedIndex = list3.Items.Count - 1;
list3.SelectedIndex = -1;
}
}
}
//--modified
//list.Items.Add(content.ToString());
content = new StringBuilder();
}
}
public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
//modified
public override void WriteLine(string text)
{
base.WriteLine(text+"½");
return;
}
//--modified
}
}