How can I force a DropDownList style ComboBox to only open when the user clicks the drop-down button?
.net-2.0, c#, combobox, winforms
Solution
After some help from the other answers, I arrived at this quick solution:
public class MyComboBox : ComboBox
{
public MyComboBox()
{
FlatStyle = FlatStyle.Popup;
DropDownStyle = ComboBoxStyle.DropDownList;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0201 /* WM_LBUTTONDOWN */ || m.Msg == 0x0203 /* WM_LBUTTONDBLCLK */)
{
int x = m.LParam.ToInt32() & 0xFFFF;
if (x >= Width - SystemInformation.VerticalScrollBarWidth)
base.WndProc(ref m);
else
{
Focus();
Invalidate();
}
}
else
base.WndProc(ref m);
}
}
Problem
In C# .NET 2.0, I have a WinForms ComboBox with ComboBoxStyle DropDownList. However, the drop down appears whenever the user clicks anywhere on the combo box. Instead, I'd like to have it only open when the user explicitly clicks on the drop down button. When the user clicks on the rest of the combo box, I'd like to just assign it the keyboard focus so he or she can use some keyboard commands on the selected item. What's the best way to do this?