Search code examples
c#vb.netwinformscomboboxcontextmenustrip

How to set default item for combox in context menu strip?


I have a context menu strip and it contains a combobox. And I want to set a default selected item for this combobox. I know get and set values for items in a context menu strip with a example as:

CtmAbc.Items(0).Text

But can't set selected item or add a event handler for this.


Solution

  • Assuming your ComboBox index is 0, you can use the code below to set the selected item as the first item:

    ((contextMenuStrip1.Items[0]) as ToolStripComboBox).SelectedIndex = 0;
    

    Or you can also access the ComboBox by key:

    ((contextMenuStrip1.Items["myComboBox"]) as ToolStripComboBox).SelectedIndex = 0;