Search code examples
c#winformstoolstrip

How to implement Visual Studio style Add or Remove Buttons toolstrip


I want to implement visual studio style Add or Remove Ruttons toolstrip like following

enter image description here

in my winforms application, how can I achieve this?

I haven't tried anything on this as I am not sure how to start and I don't have much working experience on winforms.

Please suggest.


Solution

  • At first glance it doesn't look all that difficult.

    Just add a ToolStripDropDownButton to your ToolStrip with no image or text. That will make the appearance seem more or less similar.

    Add to this drop down button one ToolStripMenuItem with a "Add or Remove Buttons" caption. We'll call it AddRemoveMenuItem.

    Now populate AddRemoveMenuItem's child menu items with menu items representing all your configurable ToolStripItems. You can link menu item and configurable tool strip item through the menu item's Tag property (you could also subclass ToolStripMenuItem adding a ToolStripItem LinkedToolStripItem { get; set; } property but I don't think its really worth it).

    All these "linked" menu items will have to handle their Click events where they will switch their linked tool strip item's Visible property and synchronize their Checked state accordingly.

    I'd do that the following way:

     linkedMenuItem.Click += (sender, e) => linkedMenuItem.Checked = !linkedMenuItem.Checked;
     linkedMenuItem.CheckedChanged +=
         (sender, e) =>
         {
             var linkedToolStripItem = linkedMenuItem.Tag as ToolStripItem;
    
             if (linkedToolStripItem != null)
             {
                 linkedToolStripItem.Visible = linkedMenuItem.Checked;
             }
         };
    

    When starting up your application set the linked menu items Checked state accordingly to your app's default settings, user settings, etc. and you are done.