Solved

Adding menu event handlers dynamically

Posted on 2006-06-12
3
301 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
  • 2
3 Comments
 
LVL 9

Accepted Solution

by:
msdixon earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now