Solved

Adding menu event handlers dynamically

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

948 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

20 Experts available now in Live!

Get 1:1 Help Now