Reputation: 4945
It seems that it works fine but in a different way than I imagined it would. If I create a simple project with a comboBox1 and a contextMenuStrip1 then use all the settings show in the image below it will display the context menu I created in the TEXT area of the dropdown just fine. I expected it to show the dropdown menu in the LIST area (with all the list items) of the dropdown.
Note: The goal here is to get the context menu ("Remove Item") to show up on top of a list item (such as "Test2").
I don't know why I'm getting hung up on something so trivial but this has stumped me for a bit now. I'm obviously doing something stupid and can't see what I'm doing wrong. My goal is simple... VIA THE DESIGN GUI, create a combobox object and a contextmenustrip object. I want the contextmenustrip object to pop up when I right click on the combobox.
I thought by just creating the combobox object then choosing the correct contextmenustrip object on the ContextMenuStrip property it would work. Seems there is more to it.
What am I doing wrong?
Upvotes: 1
Views: 7122
Reputation: 1864
This is not so easy as it seems. Because the created dropdownlist is an native ListBox window created dynamically. To access that listbox you have to send the "CB_GETCOMBOBOXINFO" in the dropdown event.
The Best way would be to derive from the System.Windows.Forms.Combobox described like here: .NET Is it possible to have a contextmenu on an item of a combobox?
A more precise answer is located here on this msdn forum. This gives you an example as well.
Upvotes: 2
Reputation: 4945
I'm not going to accept my own answer here because this is just a work around and NOT a solution to my original question. The only reason I'm posting this is in the hopes of being a quick work around for other users.
One possible work around is to not use the context menus to remove the items but instead do something similar to a lot of applications and just handle KeyDown events to use the arrow keys and delete key to remove the item from the list.
Project
and Form
(Form1).ComboBox
(comboBox1) on the newly created Form1.public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Text = "Choose from list.";
comboBox1.Items.Add("Test1");
comboBox1.Items.Add("Test2");
comboBox1.Items.Add("Test3");
}
private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete && comboBox1.DroppedDown)
{
//Remove the listitem from the combobox list
comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);
//Make sure no other processing happens, ex: deleting text from combobox
e.Handled = true;
}
else if (e.KeyCode == Keys.Down && !comboBox1.DroppedDown)
{
//If the down arrow is pressed show the dropdown list from the combobox
comboBox1.DroppedDown = true;
}
}
}
Now run the program and click on the dropdown (or tab to it) IF it's not selected. Then just use the down arrow to show the list from the combobox and select one then delete it.
Upvotes: 3