473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

how to use delegate to enable / disable mainmenu from another class

I created a mainmenu in the form class. I added the following code

public delegate void ChangeMainMenuDelegate(bool bState);

public void ChangeMainMenuState(bool bState)
{
for (int n = 0; n < mainmenu.menuitems.count; n++)
{
mainmenu.menuitems[n].Enabled = bState;
}

}

I want to call the delegate from another class (View class)

How do I do this?

Thanks

Jan 18 '07 #1
2 2642
You don't call a delegate, a delgate is a way to define the way a method
should be for an event.

So
public delegate void ChangeMainMenuDelegate(bool bState);
public event ChangeMainMenuDelegate myEvent;

then outside classes can hook the event like so: (.net 2 syntax)

myEvent += OnEventRaised;

public void OnEventRaised(bool bState) //notice it matches the params of the
delegate as it must (bool param)
{
//so when that event is fired in the class it is in this method will
also be fired
}
Make sense?
<Ab******@gmail.comwrote in message
news:11*********************@11g2000cwr.googlegrou ps.com...
>I created a mainmenu in the form class. I added the following code

public delegate void ChangeMainMenuDelegate(bool bState);

public void ChangeMainMenuState(bool bState)
{
for (int n = 0; n < mainmenu.menuitems.count; n++)
{
mainmenu.menuitems[n].Enabled = bState;
}

}

I want to call the delegate from another class (View class)

How do I do this?

Thanks

Jan 18 '07 #2
Hi,

IIRC, you only need to set the Enabled property on the containing menu and
the menu's items will inherit the value, so looping isn't required.

To invoke a delegate you need a reference to an instance of the delegate and
then you can call the Invoke method:

ChangeMainMenuDelegate method = new
ChangeMainMenuDelegate(ChangeMainMenuState);
method.Invoke(true);

or the preferred 2.0 syntax with inference, as Daniel mentioned:

ChangeMainMenuDelegate method = ChangeMainMenuState;
method.Invoke(true);

However, you don't need a delegate if its target is in scope. In that case,
just call the target method directly:

view.ChangeMainMenuState(true);

(assuming "view" is an instance of the class that defines the
ChangeMainMenuState method)

--
Dave Sexton
http://davesexton.com/blog

<Ab******@gmail.comwrote in message
news:11*********************@11g2000cwr.googlegrou ps.com...
>I created a mainmenu in the form class. I added the following code

public delegate void ChangeMainMenuDelegate(bool bState);

public void ChangeMainMenuState(bool bState)
{
for (int n = 0; n < mainmenu.menuitems.count; n++)
{
mainmenu.menuitems[n].Enabled = bState;
}

}

I want to call the delegate from another class (View class)

How do I do this?

Thanks

Jan 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: DBQueen | last post by:
I have a form with lines of controls. On some of the lines there are 3 controls (call them A,B,C); other lines have only control A. The controls have been numbered sequentially (Q20, Q21....Q76)...
0
by: Marcel | last post by:
Hi, the application I am writing has a MainMenu and a DataGrid (UltraGrid). Now I have to edit the text of each cell in this grid. So far so good. All works fine in editmode but if I press a...
6
by: Claus Holm | last post by:
I'm trying to enable a menuitem in the parent form from a mdichild. Rather than making the menuitems public, I'd go for a public method in the parent form to do the change, but when I call the...
0
by: talal | last post by:
How to check whether javascript is enable or disable in client browser. Remeber i can check wheter browser client support javascript with BrowserCapability class. But what if the client has disable...
5
by: Brian Henry | last post by:
I have a form with a main menu on it (mdi parent form) and i have a child form with a menu and I am mergeing the menus together for the menu called reports.. now when i do this they merge just...
4
by: Li Pang | last post by:
Hi, I used MID forms, when a menu is called, I don't want any menus will be called until the current one is completed. I'd like to know the way to "disable" MainMenu. I don't want to use...
8
by: rongchaua | last post by:
Hi all, i would like now to disable and enable network adapter programmatically with c#. I have searched but found nothing useful. There's no topic about this problem. Has someone done with this...
1
by: nosmil5 | last post by:
Is there a way to enable/disable a job through another job? basically this is my scenario. i have a job that runs daily to check a status of a queue. if the queue is less than 500, it quits. If...
0
by: matthewaveryusa | last post by:
Okay so I have a whole bunch of begininvokes to update my gui. I enable/disable, change the text, and all that beautiful stuff. This is, unfortunately, how my code looks right now: public...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.