c# - How to output Debug Information to a RichTextBox? -
i'm creating ftp client , wish output responses richtextbox outputting debug information.
would able on how this? thanks
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) { }
you can implement own tracelistener
gets configured in app.config , dynamically tries find richtextbox matches name in config.
your class can this:
public class textboxlistener : tracelistener { richtextbox _box; string _data; public textboxlistener(string initializedata) { _data = initializedata; } private bool init() { if (_box != null && _box.isdisposed ) { // null if control disposed _box = null; } // find logger text box if (_box == null) { // open forms foreach (form f in application.openforms) { // controls on forms foreach (control c in f.controls) { // name match if (c.name == _data && c 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; } } }
most important part of class init
method iterates on openforms , controls find richtextbox control matches name configured in app.config.
to make use of class configure tracing in app.config , add listener
<system.diagnostics> <trace> <listeners> <add name="box" type="windowsformsapplication1.textboxlistener, windowsformsapplication1" initializedata="loggerrtb" /> </listeners> </trace> </system.diagnostics>
the type qualified typename of class (namespace.classname, assemblyname) , in initializedata you'll add name of richtextbox control on form. in example app loggerrtb.
in app can use standard trace
class now:
trace.writeline("hello world");
notice textboxlistener
instantiated on first call trace.writeline
.
one of benefits of approach possibility add multiple listeners write different richtextbox controls, on separate forms (assuming forms open).
Comments
Post a Comment