By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,562 Members | 1,282 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,562 IT Pros & Developers. It's quick & easy.

Delegate requires form controls to be static?

P: n/a
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.
Nov 16 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Greg Merideth <gm**************@this.forwardtechnology.net> wrote:
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?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

P: n/a
>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?
Jon Skeet [C# MVP] wrote:
Greg Merideth <gm**************@this.forwardtechnology.net> wrote:
// 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?

Nov 16 '05 #3

P: n/a
Greg Merideth <gm**************@this.forwardtechnology.net> wrote:
>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?


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.