Solved

Adding menu event handlers dynamically

Posted on 2006-06-12
3
345 Views
Last Modified: 2010-04-16

            MainMenu mainMenu = new MainMenu();
            MenuItem fileMenuItem = new MenuItem();
            System.EventHandler temp = new System.EventHandler(fileNewMenuItem_Click);
            fileMenuItem = new MenuItem("&New", temp);
             mainMenu.MenuItems.Add(fileMenuItem);
            this.Menu = mainMenu;

 private void fileNewMenuItem_Click(Object sender, EventArgs e)
        {
            MessageBox.Show("You clicked the File -- New menu.", "The Event Information");
        }

Hi,
  How can I add a menu eent handler dynamically.I want to read the function name as a string from the table.
  this means the function name is going to be a string and so the arguments.
0
Comment
Question by:prgeorge3383
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
msdixon earned 500 total points
ID: 16889997
instead of:
System.EventHandler temp = new System.EventHandler(fileNewMenuItem_Click);

try:
fileNewMenuItem.Click += new System.EventHandler(fileNewMenuItem_Click);


check out the code that Visual Studio creates for you.
0
 

Author Comment

by:prgeorge3383
ID: 16895731
string temp = fileNewMenuItem_Click;
System.EventHandler temp = new System.EventHandler(temp);

this does not work..... I am trying to get this working
0
 

Author Comment

by:prgeorge3383
ID: 16912712
Accepted Answer from Expert1701
Date: 06/14/2006 02:35PM PDT
Grade: A
      Accepted Answer       

This should work:

  {
    string temp = "fileNewMenuItem_Click";
    EventHandler handler = (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), this, temp);

    handler(null, null);
  }

  void fileNewMenuItem_Click(object sender, EventArgs e)
  {
  }

if fileNewMenuItem_Click is a static method use,

  EventHandler handler = (EventHandler)Delegate.CreateDelegate(typeof(EventHandler), typeof(CLASS_NAME), temp);
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question