473,487 Members | 2,483 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Delegate requires form controls to be static?

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
3 1427
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
>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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
25218
by: Todd Schinell | last post by:
Back in July, Jeffery Tan posted this: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=OWOTdf0VDHA.2296%40cpmsftngxa06.phx.gbl In response as to how to get click events from a...
0
1542
by: Budi | last post by:
Hi, We try callback C# method from C++ DLL using delegate, using delegate without argument works fine for me however the CallBackFunctionWithParameter which call delegate with parameter crash...
15
5727
by: Sharon | last post by:
I’m trying to build a generic Publisher-Subscriber that will work over the net, so I’m using the Remoting. I wish that the subscriber user will be notify about the messages sent by the...
2
2345
by: Daniel Brown | last post by:
I am registering a managed delegate to an unmanaged DLL for asynchronous callback. I understand the garbage collector will free the managed resource if they go out of scope but with the...
5
2090
by: Doug Handler | last post by:
Hi, I have a form (Form1) that contains a tab control which one tab has a customer user control (UserControl1). When the user double-clicks on the grid hosted there a new user control is...
4
1395
by: Joanna Carter [TeamB] | last post by:
Hi folks I think I have asked something similar before, but here goes again... I have to call a method that varies depending on the type of one of its parameters. The object that I have to...
3
1376
by: lothar.behrens | last post by:
Hi, I am thinking about the delegate mechanism and try to understand it. I am coming from C++ and know about callbacks or member callbacks. In C++ I have this typedef for every class that...
13
14518
by: Wilfried Mestdagh | last post by:
Hi, I have an application using a DLL and callbacks. It generate random the error "A callback was made on a garbage collected delegate". I found some articles that the pointer to the delegate...
5
1388
by: ASP.NET explorer | last post by:
The following piece of code (between comment /***************/ line )works fine. You can test is in console/win form/web page. Just print strResult. ...
0
7108
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6967
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...
0
7142
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
7181
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
7352
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
3078
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...
0
1383
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
618
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
272
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.