简体   繁体   中英

Set selected Menustrip item equal to ComboBox value and vice versa

Basically I have a menustrip with 3 items. And another combobox with those exact same three items.

How do I set it so upon clicking an item on one list it sets the other list to the same value.

I hope I explained this clearly. Thanks.

If you want to set the ComboBox SelectedItem based on the MenuItem selection You can follow the below steps:

Step 1: You need to cast the sender object into ToolStripMenuItem in your ToolStripMenuItemClick Event handler.

Step 2: then assign the above casted one into ComboBox.FindString() method as an argument so that it returns the Matching Item index in the Combobox .

Step 3: now assign the returned Index value by the FindString() method to the ComboBox1.SelectedIndex property so that the exact item selected in MenuStrip willbe Selected in Combobox aswell.

Try This:

item1ToolStripMenuItem.Click += new System.EventHandler(ToolStripMenuItem_Click);
item2ToolStripMenuItem.Click += new System.EventHandler(ToolStripMenuItem_Click);
item3ToolStripMenuItem.Click += new System.EventHandler(ToolStripMenuItem_Click);

private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
  comboBox1.SelectedIndex = comboBox1.FindString(((ToolStripMenuItem)sender).Text);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM