TraceListener that outputs to a textbox

G

Guest

I want to send trace messages to a textbox in my winforms application using
C# 2.0. I have a custom TraceListener class (see below) that is very basic,
but I'v got an error implementing it that I don't understand. To instantiate
my class I have:

TextboxTraceListener TextBoxTracer = new TextboxTraceListener(txtLog);

Visual Studio underlines txtLog in blue and says:
"Error 2 A field initializer cannot reference the nonstatic field, method,
or property 'Report2085.frm2085.txtLog"

Am I trying something that simply won't work without a rewrite or is there a
simple edit I can do to fix this?


using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Windows.Forms;

namespace MyUtil
{
public class TextboxTraceListener : TraceListener
{
private TextBox textBox;
public TextboxTraceListener(TextBox textBox)
{
this.textBox = textBox;
if (!Trace.Listeners.Contains(this)) {
Trace.Listeners.Add(this); }
}

public override void Write(string message)
{
textBox.Text += message;
}

public override void WriteLine(string message)
{
textBox.Text += message + Environment.NewLine;
}

}
}
 
P

Patrick Steele

I want to send trace messages to a textbox in my winforms application using
C# 2.0. I have a custom TraceListener class (see below) that is very basic,
but I'v got an error implementing it that I don't understand. To instantiate
my class I have:

TextboxTraceListener TextBoxTracer = new TextboxTraceListener(txtLog);

Visual Studio underlines txtLog in blue and says:
"Error 2 A field initializer cannot reference the nonstatic field, method,
or property 'Report2085.frm2085.txtLog"

Where is this code executing? Where in your app is the line of code
shown above?
 
G

Guest

I believe it's in the member declarations of frm2085.cs. I'll have to answer
this question in more detail on Monday.
Should I be newing up my object within the methods in frm2085.cs that call
them?
 
G

Guest

Hi Patrick,
I've copied and pasted some code below showing how I was trying to do it on
my first try. I also tried putting it in the constructor of the form (see
"current try" below) and this was an imporovement. This way I'm not getting
any errors in the constructor. I'm now getting errors in using it.

I'm using the WriteLine method in my catch statements of various methods in
my frm2085.cs and will be using it in a few other classes. So in my form, I
have a catch statement that looks like this:

catch (Exception e)
{
TextBoxTracer.WriteLine(e.Message);
}

and I get this error when it builds:
"Error 3 The name 'TextBoxTracer' does not exist in the current context"

Should the lines I now have in my constructor be cut and pasted into each of
my catch statements or is there something else I should try?


///////////////////////////////////////////////////////
// first try
////////////////////////////////////////////////////
public partial class frm2085 : Form
{
private Excel.ApplicationClass _excel;
private Excel.Workbook _book;
protected Excel.Sheets _sheets;
protected Excel.Worksheet _sheet;
private Int32 headerRow;
TextboxTraceListener TextBoxTracer = new TextboxTraceListener(txtLog);
Trace.Listeners.Add(TextBoxTracer);

public frm2085()
{
InitializeComponent();
// create a new COM object for excel
_excel = new ApplicationClass();
}

///////////////////////////////////////////////////////
// current try
////////////////////////////////////////////////////
public frm2085()
{
InitializeComponent();
// create a new COM object for excel
_excel = new ApplicationClass();
TextboxTraceListener TextBoxTracer = new
TextboxTraceListener(txtLog);
Trace.Listeners.Add(TextBoxTracer);
}
 
P

Patrick Steele

Hi Patrick,
I've copied and pasted some code below showing how I was trying to do it on
my first try. I also tried putting it in the constructor of the form (see
"current try" below) and this was an imporovement. This way I'm not getting
any errors in the constructor. I'm now getting errors in using it.

I'm using the WriteLine method in my catch statements of various methods in
my frm2085.cs and will be using it in a few other classes. So in my form, I
have a catch statement that looks like this:

catch (Exception e)
{
TextBoxTracer.WriteLine(e.Message);
}

and I get this error when it builds:
"Error 3 The name 'TextBoxTracer' does not exist in the current context"

Once you add a listener to the Listeners collection, any calls to
Trace.WriteLine should be sent to all listeners. You don't need to
worry about accessing an individual listener (like the one you've
written).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top