Delegate requires form controls to be static?

  • Thread starter Thread starter Greg Merideth
  • Start date Start date
G

Greg Merideth

An app I'm working on has a "message" box docked to the bottom of the window to
display messages. I've added a series of child windows to the main form and
wanted to keep passing messages into the docked window. The main app has a
method to take the string, format it and then add it to the rich text box.

I created a delegate to be able to call the main methods RichTextAddLine but in
order to get it to work I had to set the richtextBox static. The compiler
would tell me that it was expecting a class and a field was given in the
RichTextAddLine method after I made the method public static call.

Is there a better way to create the delegate in the second class to be able to
call the RichTextAddLine method without making it and every window form item it
uses static?

Here's what I'm using now

// in the class definition
private delegate void RichTextWriteOP(string _lpMessage);
RichTextWriteOP RichTextWriter = new RichTextWriteOP(_frmMain.RichTextAddLine);

and in the _frmMain class
public static void RichTextAddLine(string _slpMessage)
{
...write to the screen's rich text box element
}

It works now as long as I make the method and the form elements that interact
with this method static.

Thanks for any advice.
 
Greg Merideth said:
An app I'm working on has a "message" box docked to the bottom of the window to
display messages. I've added a series of child windows to the main form and
wanted to keep passing messages into the docked window. The main app has a
method to take the string, format it and then add it to the rich text box.

I created a delegate to be able to call the main methods RichTextAddLine but in
order to get it to work I had to set the richtextBox static. The compiler
would tell me that it was expecting a class and a field was given in the
RichTextAddLine method after I made the method public static call.

Is there a better way to create the delegate in the second class to be able to
call the RichTextAddLine method without making it and every window form item it
uses static?

Here's what I'm using now

// in the class definition
private delegate void RichTextWriteOP(string _lpMessage);
RichTextWriteOP RichTextWriter = new RichTextWriteOP(_frmMain.RichTextAddLine);

and in the _frmMain class
public static void RichTextAddLine(string _slpMessage)
{
...write to the screen's rich text box element
}

It works now as long as I make the method and the form elements that interact
with this method static.

You need to have an instance - eg _frmMain - to add to. Otherwise how
is it meant to know which form you're talking about?
 
You need to have an instance - eg _frmMain - to add to.

The _frmMain is the base form of the application. It has all of the windows
controls on it. I've added a new form to the project which _frmMain creates as
a child form within itself. The delegate I've created references back to the
RichTextAddLine method of the _frmMain class.

It's working but when you say "add to" do you mean that I need to provide a
method override in my child (MDI) class?
 
Greg Merideth said:
The _frmMain is the base form of the application. It has all of the
windows controls on it. I've added a new form to the project which
_frmMain creates as a child form within itself. The delegate I've
created references back to the RichTextAddLine method of the _frmMain
class.

It's working but when you say "add to" do you mean that I need to provide a
method override in my child (MDI) class?

Not necessarily.

I don't understand your particular situation, but the fact that you say
something will only work with static methods/members suggests that you
just need to use a reference to get it to work with instance methods.

Perhaps if you could provide a short but complete example I could help
more.

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Back
Top