Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Adding menu event handlers dynamically

Posted on 2006-06-12
3
Medium Priority
?
352 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 1500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

810 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