473,618 Members | 3,005 Online
Bytes | Software Development & Data Engineering Community
+ 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.RichT extAddLine);

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 1433
Greg Merideth <gm************ **@this.forward technology.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.RichT extAddLine);

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.co m>
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.forward technology.net> wrote:
// in the class definition
private delegate void RichTextWriteOP (string _lpMessage);
RichTextWrite OP RichTextWriter = new RichTextWriteOP (_frmMain.RichT extAddLine);

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.forward technology.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.co m>
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
25262
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 user control to invoke a method in the parent form. This code doesn't seem to work for me, I've tried it a number of times with very simple test cases (A user control with a single button and a parent form with a single text box) and it always...
0
1554
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 the application after being called . Any suggestion will help and really appreaciated Thanks
15
5742
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 remote publisher, so I used delegate that the user will be able to set on it his own function for that purpuse. The trouble is that this delegate must not be static because there may be many subscribers, and each subscriber may have different...
2
2354
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 information I have read, it is unclear to me if the garbage collector will move the delegate (e.g. cableInterruptCallbackEventHandler) if it is still referenced thereby invalidating the callback performed by the unmanaged code. I would greatly appreciate...
5
2099
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 created. I have a delegate / event that passes via EventArgs the appropriate info from the grid to the UserControl2. This works fine, except for now I'm a little lost on the final step....I need that once the UserControl2 is "filled out" that it is...
4
1407
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 pass is of a base class. I do not want to have an "if (...) else" construct if I can avoid it.
3
1397
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 implements member callbacks: typedef bool (__cdecl IEventHandler::*lbEvHandler)(IUnknown* uk);
13
14546
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 has to have a lifetime reference so that it is not garbage collected. But I could not find any example how to do this. Can someone help here ?
5
1393
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. /**************************************************/ public delegate double DlgFn(double x); public static double f1(double x) { return Math.Exp(-(x * x)); //function to be integrated by Simpson's rule }
0
8212
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8153
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
8595
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8455
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
7126
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...
0
5552
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
4150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2587
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 we have to send another system
1
1760
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.