473,398 Members | 2,188 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

TraceListener that outputs to a textbox

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

}
}
Feb 14 '07 #1
4 6500
In article <47**********************************@microsoft.co m>,
ar*********@discussions.microsoft.com says...
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?

--
Patrick Steele
http://weblogs.asp.net/psteele
Feb 16 '07 #2
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?
"Patrick Steele" wrote:
In article <47**********************************@microsoft.co m>,
ar*********@discussions.microsoft.com says...
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?

--
Patrick Steele
http://weblogs.asp.net/psteele
Feb 17 '07 #3
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);
}
"Patrick Steele" wrote:
In article <47**********************************@microsoft.co m>,
ar*********@discussions.microsoft.com says...
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?

--
Patrick Steele
http://weblogs.asp.net/psteele
Feb 19 '07 #4
In article <D1**********************************@microsoft.co m>,
ar*********@discussions.microsoft.com says...
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).

--
Patrick Steele
http://weblogs.asp.net/psteele
Feb 21 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Daylor | last post by:
hi. i have application with 5 threads. can i use the trace and tracelistener, so if i trace in thread1 , it will write to file1, and if i trace to thread2 if will write to file2. hope im...
0
by: Schorschi | last post by:
Anyone create a custom tracelistener that redirects to a database? Or event to a memory stream rather than a file stream? If an example of a memory stream, when the Write or WriteLine methods...
2
by: John Batdorf | last post by:
Ugh. I obviously don't understand the correct way to use this thing! I have several #if(DEBUG) code blocks to write to a file ErrorLog.txt to troubleshoot why and what exceptions are being...
3
by: Mullin Yu | last post by:
i have a problem with TraceListener. when i close the FileStream, i will have the error: "Cannot access a closed file" but, if i remarks the statement => no closing the FileStream, i will have...
0
by: bonk | last post by:
I have an application that initializes a lot of tracesources at runtime. The names of these tracesources are only known at runtime. How can I add one tracelistener to all tracesources using the...
2
by: john sun | last post by:
Hi, Dear gurus, In the web deployment project, there is one option called "Output Assemblies page". The first choice is "Merge all outputs to a single assembly", According to MSDN, "Merges...
7
by: =?Utf-8?B?VGhlIE1hbiBGcm9tIFNRTA==?= | last post by:
I'm having the darndest problem and I can't seem to recreate it anywhere, and I'm looking for someone with a little insight. I've created a custom EmailTraceListener which implements the...
2
by: Ken Williams | last post by:
In general the program is GUI based. However sometimes I would like to wirte some text lines to Console as if it is a Console program. When I write a System.ConsoleWriteLine() statement this...
7
TheServant
by: TheServant | last post by:
Hey guys, I have an untraining script in my game which I am having a small problem with. There are 4 levels of soldiers: rec, reg, exp and vet. Now the number of soldier is recorded as the total, so...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.