Search code examples
c#eventhandlertoolstripbutton

How to add a event handler to a ToolStripButton in C# code?


Me again, and it's another problem with my plugin parser for my c# browser, I'm trying to add a eventhandler to make it to where when you hit the plugin button it does something. The reason i am having to do this in code is because it is loading the plugins from files, and they are not hard-coded. Here's my code, It will look pretty familar to the last one if you saw it

toolStrip1.Items.Add( pluginButton );
pluginButton.Image = Simple_Browse.Properties.Resources.plugin;
pluginButton.Alignment = ToolStripItemAlignment.Right;
pluginButton.ToolTipText = TitlePlugin;
pluginButton.Click += StartPlugin();
private EventHandler StartPlugin()
{

    PluginPage plgp = new PluginPage();
    plgp.Show();
    plgp.Text = PlgTitle2;

}

So the code is pretty basic, but im getting an error at private EventHandler StartPlugin() The error is not all code paths return a value Please help!


Solution

  • You probably meant to do this instead:

    pluginButton.Click += StartPlugin; // not StartPlugin()
    private void StartPlugin(object sender, EventArgs e)
    {
        PluginPage plgp = new PluginPage();
        plgp.Show();
        plgp.Text = PlgTitle2;
    }
    

    It looks like you may need to read a bit more on how delegates and event handlers work.