R
Russell Smallwood
Hey all,
Okay -
I'm trying to get a handle on setting up a custom event, as you can
probably
tell by the code below, I'm struggling.
Some background. What I'm trying to do is update the text in an rtf
control on my main form whenever something is written to the logfile.
Since I'm writing
to the log all over my application, I want to keep this rtf control and
the events that publish their verbage to the log loosely coupled. This
sounded like
the perfect application of the event model in the .NET framework.
As a sidebar, It sounds as if events and delegates are the ideal way to
pass information around your application. In other words, If the user
is sitting on
a form, then does something that calls another class, it seems that an
event/subscriber conversation is the perfect way to pass data back to
the main form.
Am I thinking correctly?
Anyway, back to my question ----
Somewhere, deep within the bowls of my Windows forms application, I have
this:
public class loggerEventArgs : EventArgs
{
public readonly string logData;
public readonly string logCaller;
public loggerEventArgs (string logString, string caller)
{
this.logCaller=caller;
this.logData=logString;
}
}
public class writeLog
{
private string _logData;
private string _logCaller;
public delegate void logHandler(object logstuff,
loggerEventArgs theseArgs);
public event logHandler OnlogWrite;
public writeLog(string logData, string caller)
{
this._logData=logData;
this._logCaller=caller;
}
public void publish()
{
loggerEventArgs thislogevent = new
loggerEventArgs(this._logData,this._logCaller);
if(OnlogWrite != null)
{
OnlogWrite(this,thislogevent);
}
}
}
Elsewhere in another class
writeLog mylog=new writeLog("Doing some stuff","stuff doer");
mylog.publish();
So far so good (I think). However, now how to I subscribe in my form
class?
I'm doing some stuff in my form's code that starts this whole process
off in the first place..
In other words, on my main form, I have a rich text control that I'm
using to display the stuff I'm
writing to my log, so I want to subscribe to the event somewhere in my
form's code.
This is what I would have expected to do:
writeLog.OnlogWrite += new writeLog.logHandler(updateLogWindow);
for which I would write a method called updateLogWindow which updated my
Rich Text control but this doesn't work.
Why? It appears as though I need a reference to the writeLog object,
but that doesn't make sense to me.
help.
-r
---------------------
--
Russell Smallwood
Relatia Software Corporation
Finally! Time and Billing in GoldMine
www.relatia.net
Okay -
I'm trying to get a handle on setting up a custom event, as you can
probably
tell by the code below, I'm struggling.
Some background. What I'm trying to do is update the text in an rtf
control on my main form whenever something is written to the logfile.
Since I'm writing
to the log all over my application, I want to keep this rtf control and
the events that publish their verbage to the log loosely coupled. This
sounded like
the perfect application of the event model in the .NET framework.
As a sidebar, It sounds as if events and delegates are the ideal way to
pass information around your application. In other words, If the user
is sitting on
a form, then does something that calls another class, it seems that an
event/subscriber conversation is the perfect way to pass data back to
the main form.
Am I thinking correctly?
Anyway, back to my question ----
Somewhere, deep within the bowls of my Windows forms application, I have
this:
public class loggerEventArgs : EventArgs
{
public readonly string logData;
public readonly string logCaller;
public loggerEventArgs (string logString, string caller)
{
this.logCaller=caller;
this.logData=logString;
}
}
public class writeLog
{
private string _logData;
private string _logCaller;
public delegate void logHandler(object logstuff,
loggerEventArgs theseArgs);
public event logHandler OnlogWrite;
public writeLog(string logData, string caller)
{
this._logData=logData;
this._logCaller=caller;
}
public void publish()
{
loggerEventArgs thislogevent = new
loggerEventArgs(this._logData,this._logCaller);
if(OnlogWrite != null)
{
OnlogWrite(this,thislogevent);
}
}
}
Elsewhere in another class
writeLog mylog=new writeLog("Doing some stuff","stuff doer");
mylog.publish();
So far so good (I think). However, now how to I subscribe in my form
class?
I'm doing some stuff in my form's code that starts this whole process
off in the first place..
In other words, on my main form, I have a rich text control that I'm
using to display the stuff I'm
writing to my log, so I want to subscribe to the event somewhere in my
form's code.
This is what I would have expected to do:
writeLog.OnlogWrite += new writeLog.logHandler(updateLogWindow);
for which I would write a method called updateLogWindow which updated my
Rich Text control but this doesn't work.
Why? It appears as though I need a reference to the writeLog object,
but that doesn't make sense to me.
help.
-r
---------------------
--
Russell Smallwood
Relatia Software Corporation
Finally! Time and Billing in GoldMine
www.relatia.net