473,799 Members | 2,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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:

TextboxTraceLis tener TextBoxTracer = new TextboxTraceLis tener(txtLog);

Visual Studio underlines txtLog in blue and says:
"Error 2 A field initializer cannot reference the nonstatic field, method,
or property 'Report2085.frm 2085.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.Collecti ons.Generic;
using System.Text;
using System.Diagnost ics;
using System.Windows. Forms;

namespace MyUtil
{
public class TextboxTraceLis tener : TraceListener
{
private TextBox textBox;
public TextboxTraceLis tener(TextBox textBox)
{
this.textBox = textBox;
if (!Trace.Listene rs.Contains(thi s)) {
Trace.Listeners .Add(this); }
}

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

public override void WriteLine(strin g message)
{
textBox.Text += message + Environment.New Line;
}

}
}
Feb 14 '07 #1
4 6527
In article <47************ *************** *******@microso ft.com>,
ar*********@dis cussions.micros oft.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:

TextboxTraceLis tener TextBoxTracer = new TextboxTraceLis tener(txtLog);

Visual Studio underlines txtLog in blue and says:
"Error 2 A field initializer cannot reference the nonstatic field, method,
or property 'Report2085.frm 2085.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************ *************** *******@microso ft.com>,
ar*********@dis cussions.micros oft.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:

TextboxTraceLis tener TextBoxTracer = new TextboxTraceLis tener(txtLog);

Visual Studio underlines txtLog in blue and says:
"Error 2 A field initializer cannot reference the nonstatic field, method,
or property 'Report2085.frm 2085.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.W riteLine(e.Mess age);
}

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.Applicati onClass _excel;
private Excel.Workbook _book;
protected Excel.Sheets _sheets;
protected Excel.Worksheet _sheet;
private Int32 headerRow;
TextboxTraceLis tener TextBoxTracer = new TextboxTraceLis tener(txtLog);
Trace.Listeners .Add(TextBoxTra cer);

public frm2085()
{
InitializeCompo nent();
// create a new COM object for excel
_excel = new ApplicationClas s();
}

///////////////////////////////////////////////////////
// current try
////////////////////////////////////////////////////
public frm2085()
{
InitializeCompo nent();
// create a new COM object for excel
_excel = new ApplicationClas s();
TextboxTraceLis tener TextBoxTracer = new
TextboxTraceLis tener(txtLog);
Trace.Listeners .Add(TextBoxTra cer);
}
"Patrick Steele" wrote:
In article <47************ *************** *******@microso ft.com>,
ar*********@dis cussions.micros oft.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:

TextboxTraceLis tener TextBoxTracer = new TextboxTraceLis tener(txtLog);

Visual Studio underlines txtLog in blue and says:
"Error 2 A field initializer cannot reference the nonstatic field, method,
or property 'Report2085.frm 2085.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************ *************** *******@microso ft.com>,
ar*********@dis cussions.micros oft.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.W riteLine(e.Mess age);
}

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
2209
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 clear, have a nice day.
0
1295
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 get called I could redirect/parse the text to a database? Not sure of the best way to do this, any suggestions welcome.
2
2000
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 encountered. So in my class this is defined: static FileStream traceLog = new FileStream("ErrorLog.txt", FileMode.OpenOrCreate, FileAccess.Write); static TextWriterTraceListener traceListener = new
3
6862
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 another error: "The process cannot access the file "C:\MyAppsTraceLog.log" because it is being used by another process." Remind that it's ok when i click the button once. the problem arises when
0
1096
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 config file (since I do not own the sourcecode for some of the assemblies that generate these tracelisteners)? If that is not possible, how can I collect all tracemessages into one file? I am using .net framework 2.0.
2
3304
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 all the output assemblies from the compiler into a single assembly. This setting is equivalent to the -o assemblyname option of
7
1906
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 TraceListener abstract class. The EmailTraceListener stores all of the messages in a StringBuilder internally and then writes the StringBuilder.ToString() to an email as a text message. Here's the problem I'm encountering. I load up several...
2
3270
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 is accepted without error (during build and runtime). Unfortunately no Cosole pops up at runtime and showing the text. I assume I have to enable the Console resp. make it visible and let it popup when necessary.
7
1376
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 when a user "untrains" some, it will take it from the total. I want to remove rec's first, reg's second, etc... Now I think the function works, but I just don't know how to do inputs and outputs. I want to automatically assign the outputs to...
0
9540
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10475
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10026
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.