Results 1 to 4 of 4

Thread: Adding EventHandler - am I doing this right?

  1. #1

    Thread Starter
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690

    Adding EventHandler - am I doing this right?

    I'm trying to build a context menu on the fly and add an EvendHandler so something happens when I click my MenuItem. Problem is, nothing happens when I click the MenuItem. Am I missing something in this code?
    Code:
    ContextMenu cm = new ContextMenu();
    MenuItem miNewRadioMessage = new MenuItem("&Radio Message");
    this.miNewRadioMessage.Click += new System.EventHandler(this.CreateRadioMessage);
    cm.MenuItems.Add(miNewRadioMessage);
    cm.Show(axMap, new Point(mouseX, mouseY));
    
    private void CreateRadioMessage(object sender, EventArgs e)
    {	
    	MessageBox.Show("click");
    }
    Thanks,
    Mike

  2. #2
    <?="Moderator"?> john tindell's Avatar
    Join Date
    Jan 2002
    Location
    Brighton, UK
    Posts
    1,099
    I don't known if this affects it but all the examples I have seen state the action in the function name

    eg
    Code:
    this.mnuFile_Exit.Click += new System.EventHandler(this.mnuFile_Click);
    
    /* menu event code */
    private void mnuFile_Click(object sender, EventArgs e)
    {
        MessageBox.Show("You Click the File Menu Item!");
    }

  3. #3
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    you could always try inheriting the menu items in a custom class then add your events in it , also you need to trigger the event before anything will happen , eg:
    PHP Code:
        public class mnuItem MenuItem
        
    {
            public 
    delegate void createMessage(object senderEventArgs e);
            public 
    event createMessage CreateRadioMessage;
            
            public 
    void CreateMessage(object senderEventArgs e)
            {
                
    CreateRadioMessage(sender e);
            }
        } 
    to add a menuitem / test it in a form ...
    PHP Code:
            private void button1_Click(object senderSystem.EventArgs e)
            {
                
    mnuItem mnu = new mnuItem();
                
    mnu.Text "i'm a menu item";
                
    mnu.CreateRadioMessage +=new inherit_menu.mnuItem.createMessage(mnu_CreateRadioMessage);
                
    contextMenu1.MenuItems.Add(mnu);
                
    mnu.CreateMessage((object)"test",null);
            }
            private 
    void mnu_CreateRadioMessage(object senderEventArgs e)
            {    
                
    MessageBox.Show(Convert.ToString(sender));
            } 
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  4. #4

    Thread Starter
    Frenzied Member Mike Hildner's Avatar
    Join Date
    Jul 2002
    Location
    Des Moines, NM
    Posts
    1,690
    Thanks for the replies. I'm not sure what's going on, but when I change the owner of the menu from "axMap" - which is a MapPoint ActiveX control to "this", things seem to work fine.

    Thanks,
    Mike

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width