简体   繁体   中英

How can i put an event on a context menu item?

I work at a program which needs some tabs.I didn't want to put buttons to add/remove tabs, my idea was to add a context menu when i press right click but it gives me an error. This is the code:

private void tabControl1_MouseDown(object sender, MouseEventArgs e)
    {
        if (context_add_remove_control < 1)
        {
            var add_tab = tab_options.MenuItems.Add("Add tab");
            var remove_tab = tab_options.MenuItems.Add("Remove tab");
            add_tab.Click += new EventHandler (Add_tab_click());
        }
        tabControl1.ContextMenu = tab_options;
    }

    private EventHandler Add_tab_click()
    {

    }

But the Add_tab_click method gives me an error:

'Form1.Add_tab_click': not all code paths return a value

What this means?

private EventHandler Add_tab_click()

The compiler is expecting Add_tab_click() to return an EventHandler because your method is marked as returning one (as opposed to void). In this case however, the method SHOULD return void because the EventHandler delegate expects a method that takes two parameters (an object and EventArgs) and returns void. So you should change your code to:

private void Add_tab_click(object sender, EventArgs e)

and change this: add_tab.Click += new EventHandler (Add_tab_click());

to:

add_tab.Click += Add_tab_click;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM