Search code examples
c#comboboxcontextmenustrip

Using A ContextMenuStrip On A ComboBox


Update:

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.

Dropdown SampleDropdown Sample MenuDropdown Sample Menu Fail

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?

Context Menu Example


Solution

  • 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.