Solved

Adding menu event handlers dynamically

Posted on 2006-06-12
3
336 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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

763 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