【问题标题】:How to output Debug Information to a RichTextBox?如何将调试信息输出到 RichTextBox?
【发布时间】:2016-05-18 07:58:09
【问题描述】:

我正在创建一个 FTP 客户端,我希望通过输出调试信息将响应输出到 RichTextBox。

有没有人能帮忙解决这个问题?谢谢

public FileARK()
{
    InitializeComponent();
    hostAddress.Text = "host";
    UserName.Text = "foo";
    Password.Text = "bar";
    //FtpTrace.AddListener(new TextWriterTraceListener(responseWindow.Text));
   // Debug.WriteLine(responseWindow.Text);
}

private void Connect_Click(object sender, EventArgs e)
{
    conn = new FtpClient();
    conn.Host = hostAddress.Text;
    conn.Credentials = new NetworkCredential(UserName.Text, Password.Text);
    conn.Connect();


}

private void Disconnect_Click(object sender, EventArgs e)
{
    conn.Disconnect();
}

private void responseWindow_TextChanged(object sender, EventArgs e)
{

}

【问题讨论】:

  • 问题是什么?
  • 到目前为止你做了什么?
  • 我创建了一个可以连接到 ftp 服务器的 ftp 客户端,我在调试窗口中输出了对服务器的响应,但我想知道如何获取调试数据并输出它在应用程序运行时放入 RichTextBox。
  • 也许你应该向我们展示你目前拥有的代码.. 谷歌搜索如何使用Debug.WriteLine
  • RichTextBox 对于您想要的可能有点过分,但您要调用的属性是 myCoolRichTextBox.Document = myCoolDocument。见这里:msdn.microsoft.com/en-us/library/…

标签: c# richtextbox system.diagnostics


【解决方案1】:

您可以实现自己的TraceListener,该TraceListener 在 app.config 中进行配置,并动态尝试查找与配置中的名称匹配的富文本框。

您的班级可能如下所示:

public class TextBoxListener : TraceListener
{
    RichTextBox _box;
    string _data;
    public TextBoxListener(string initializeData)
    {
        _data = initializeData;
    }

    private bool Init()
    {
        if (_box != null && _box.IsDisposed )
        {
            // back to null if the control is disposed
            _box = null;
        }
        // find the logger text box
        if (_box == null)
        {
            // open forms
            foreach (Form f in Application.OpenForms)
            {
                // controls on those forms
                foreach (Control c in f.Controls)
                {
                    // does the name match 
                    if (c.Name == _data && c is RichTextBox)
                    {
                        // found one!
                        _box = (RichTextBox) c;
                        break;
                    }
                }
            }
        }
        return _box != null && !_box.IsDisposed;
    }

    public override void WriteLine(string message)
    {
        if (Init())
        {
            _box.Text = _box.Text + message + "\r\n";
        }
    }

    public override void Write(string message)
    {
        if (Init())
        {
            _box.Text = _box.Text + message;
        }
    }
}

这个类最重要的部分是 Init 方法,它遍历所有 openforms 和所有控件以找到与 app.config 中配置的名称匹配的富文本框控件。

要使用此类,请在您的 app.config 和 add a listener 中配置跟踪

<system.diagnostics>
    <trace>
      <listeners>
        <add name="box" 
             type="WindowsFormsApplication1.TextBoxListener, WindowsFormsApplication1" 
             initializeData="loggerRTB" />
      </listeners>
    </trace>
  </system.diagnostics>

类型是类的完全限定类型名(Namespace.Classname、AssemblyName),在initializeData 中,您将在表单上添加richtextbox 控件的名称。在我的示例应用中,它是 loggerRTB

您现在可以在您的应用中使用标准的Trace 类:

Trace.WriteLine("Hello world");

请注意,TextBoxListener 仅在第一次调用 Trace.WriteLine 时被实例化。

这种方法的一个好处是可以添加多个写入不同 RichTextBox 控件的侦听器,即使是在单独的表单上(假设这些表单是打开的)。

【讨论】:

  • 谢谢你!,我的一切都按我的需要工作了 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-10-01
  • 1970-01-01
  • 2017-05-19
  • 1970-01-01
  • 1970-01-01
  • 2015-08-28
  • 2018-05-20
相关资源
最近更新 更多