How to prevent ComboBox auto DropDown
Hi,
I'm writing a custom CheckedComboBox with a multiple item select future but when I select an item from the dropdown menu, the dropdown menu automatically closes. How can I keep it open to select more than one item? thanks for any help.
Hi Bluehunter,
It looks like you are trying to make a custom combobox based on the System.Windows.Forms.ComboBox. Without access to the underlying code this might be difficult.
Remember that a combobox basically presents a listbox after pressing on a button.
So if you are making your own combobox, you can have your button present a listbox by your choice. Of course this can be a multiple select listbox.
This way it's you that determines what happens on the listbox events. So you can keep it open as long as you like.
An alternative way to close the listbox and present the chosen items will have to be provided.
Regards, Tonn
Hi tonn,
Thanks for your reply but I do not use any ListBox in my CheckedComboBox to keep it lightweight. I use a Hasthtable called _checkedItems and by overriding OnSelectedIndexChanged event, I add this list the item itself as a key, and its index(not used in any part of the code because when my CheckedListBox is sorted then the indexes change dynamically) as a value. I had a look at some events CB_GETDROPPEDSTATE and CB_SHOWDROPDOWN but I'm not able to cancel any of these messages. Any idea?
You've got me puzzled. I think I need more information, here are my issues:
What you call a dropdown menu is actually a dropped down listbox built into the combobox. Could it be that you're not as lightweight as you think you are?
You are basically recording user clicks into a hashtable. Why not use a listbox instead of a combobox?
You suddenly mention CheckedListBox instead of CheckedComboBox like you did before. Do we have a miscommunication?
CB_GETDROPPEDSTATE and CB_SHOWDROPDOWN are not events, they are windows messages. Do you really need to go that low level?
It's not possible to cancel windows messages, don't bother trying.
The big one: are you doing Windows CE? Because I'm not a CE programmer and perhaps not qualified to address your problem.
I wrote it wrong, sorry, my custom control is CheckedComboBox. I don't want to use a CheckedListBox or a ListBox because I need a one which will open and close like a ComboBox but will have MultipleSelect and CheckBox futures which makes it suitable for my application. I know that the dropped part is a ListBox and I also learnt ( 5min. ago ) that if I want to dive into WndProc of this ListBox, I should catch WM_CTLCOLORLISTBOX event and then subclass it. If you know how to do the rest you can help me writing it out. After I finish with it, I will put it free for use to MSDN, so any help to make me finish it quickly would be nice. Also,I don't record using clicks, I record SelectedItems when the clicked area is the CheckBox rectangle on the left site of the item text. So, even if the list is sorted, I can easily reach any item in the hastable of _checkedListBox without any error. Nice, isn't it?
Without wanting to be pushy, I still think I should point out to you that a combobox (any comobobox) is nothing more than a fancy way to popup and popdown a listbox (any listbox). Writing code to do that is easy. Wrapping that code into a Control using C# is very easy.
What you call catching messages is actually writing your own message-handlers. I did a lot of that in my Win32 / Delphi period. Be aware that this is never a trivial matter.
At the moment I do not have access to the underlying code, so I can't help you with your preferred solution.
I hope you find a way to do it. I would be very interested in the result.
Good luck.
Hi, thanks for your help effort again. After a lot of search about the steps I need to take to complete my CheckedComboBox, I found out some codes in MFC but I could not convert some of the code to C#. I could do it importing the dll itself and forgetting the rest but I'm really interested in solving this code so that I can add some extra functionality to it. The original code is from http://www.codeproject.com/combobox/CheckComboBox2.asp
Below is one of the difficult parts I was not able to convert to C# and if you could send the me the C# of below code and what it really does in details, I would be very glad. Thanks for any help.
ON_CONTROL_REFLECT_EX(CBN_DROPDOWN, OnDropDown)
I think that you want something like this:
protected override void WndProc(ref Message m)
{
if (m.Msg == (WM_REFLECT + WM_COMMAND))
{
if (HIWORD((int)m.WParam) == CBN_DROPDOWN)
{
ShowDropDown();
return;
}
}
base.WndProc(ref m);
}
public static int HIWORD(int n)
{
return (n >> 16) & 0xffff;
}