Search code examples
c#winformstoolstriptoolstripdropdown

Add object other than string to ToolStripDropDownButton DropDownItem


It's easy to add strings to a ToolStripDropDownButton as a new DropDownItem. What I need is to add a custom object to the DropDownItems so that I can assign a key/value like object to the DropDownItem.

How may I achieve this?


Solution

  • ToolStripItems can only display text and images and don't take objects. You can try sneaking an object in the Tag property of the button:

      ToolStripDropDownButton b = new ToolStripDropDownButton();
      b.DropDownItems.Add(new ToolStripButton("Hello") { Tag = new Something() });
    

    and then when you handle the click event, inspect the Tag property for your object.