Search code examples
c#winforms.net-coreclicktoolstrip

Problem with Toolstrip WinForms .Net Core


I'm making a Winforms application in .Net Core to learn some basic stuff. It's a simple application where I write some text from a textbox to a file (basically like notepad).

Now I wanted to add a toolstrip with a toolstripbutton to make a new file and a toolstripbutton to save the text to the file, etc..

So I add the toolstrip from toolbox and drag it to my form1.cs[design]. Then I right click on the toolstrip and click "Insert standard items". The buttons are being added to the toolstrip.

Now my problem is I want to add a click event to the buttons but I can't double click the toolstripbuttons individually. So I have to go into the designer.cs to add the click event. For example:

this.newToolStripButton.Click += new System.EventHandler(this.btnNewFile_Click);

I just tried it with a WinForms app in .Net Framework and there I can just double click the toolstripbuttons to add an eventhandler.

Do I have to take a different approach in .Net Core? Is this not yet fully supported in .Net Core and should I just use .Net Framework for Winforms? I read that not all of the functionalities from Winforms in .Net Framework are yet supported in .Net Core, but Toolstrip should work.

Thanks in advance


Solution

  • As of Visual Studio 2019 16.8.0 Preview 6.0 (and also VS2019 16.7.6), double-clicking a ToolStripButton in the designer will launch code for the ToolStrip_ItemClicked event. In that event you can check the ToolStripItemClickedEventArgs.ClickedItem to see what was pressed.

    private void toolStrip2_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
            {
                ToolStripButton tsb = (ToolStripButton)e.ClickedItem;
            }
    

    If you want to get to the Click event for an individual ToolStripButton, you can select it in the Properties window, click the Events button, and insert it there.