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;
}
}
}
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;
}
}
}