So say you want to have a Trace listener setup to listen for any Trace outputs and you would like to take the Trace Write outputs and put that into a listbox.
This shows you how to setup a custom eventargs, custom TraceListner and then add the custom Trace listener to add messages received to a listbox.
Make a new custom EventArgs like this:
[code:c#] public class EventListenerArgs : EventArgs
{
private string _message;
public string Message
{
get { return _message; }
set { _message = value; }
}
public EventListenerArgs(string Message)
{
this.Message = Message;
}
}[/code]
Then setup a custom TraceListner that implements the EventListener above like this;
[code:c#] public class EventFiringListener : TraceListener
{
public event EventHandler<EventListenerArgs> WriteEvent;
public override void WriteLine(string message)
{
EventHandler<EventListenerArgs> temp = WriteEvent;
if (temp != null)
{
temp(this, new EventListenerArgs(message));
}
}
public override void Write(string s)
{
EventHandler<EventListenerArgs> temp = WriteEvent;
if (temp != null)
{
temp(this, new EventListenerArgs(s));
}
}
}
[/code]
Then in your form (where you have the listbox) create the custom TraceListener and add it to the TraceListener collections;
[code:c#]
EventFiringListener listener = new EventFiringListener();
listener.WriteEvent += new EventHandler<EventListenerArgs>(listener_WriteEvent);[/code]
And the method event could look like this;
[code:c#] void listener_WriteEvent(object sender, EventListenerArgs e)
{
this.listbox.items.add(e.Message);
}
[/code]
Enjoy!