GuidoG
GuidoG

Reputation: 12059

winforms combobox, how to stop it from dropping down when getting focus

I have a combobox on a winform with dropdownstyle set to DropDownList.

When the user clicks anywhere on the combobox, its dropdownlist opens up. If I use any other dropdownstyle (DropDown or Simple) this is not the case, the combobox will only open up when the user clicks on the arrow on the right.

What I need is a combobox that has dropdownstyle set to DropDownList but still only opens its dropdown list when clicking on the arrow on the right, not when clicking anywhere else on the combobox, just like it does when dropdownstyle is DropDown or Simple.

In case you wondering why I want this, I have DrawMode set to OwnerDrawFixed and in the DrawItem I draw the combobox so it looks normal, not the ugly 3d that this dropdownstyle forces upon me. So I actually have a readonly combobox but without the ugly 3d look.

If requested I can post the code from the DrawItem, but this code does not has any influence on this behaviour because without the drawitem code the combobox reacts exactly the same.

I hope this question is clear enough.

Upvotes: 1

Views: 942

Answers (1)

GuidoG
GuidoG

Reputation: 12059

My good friend Google came to the resque, this piece of code seems to fix my problem:

const int WM_LBUTTONDOWN = 0x0201;
const int WM_LBUTTONDBLCLK = 0x0203;

protected override void WndProc(ref Message m)
    {
        // only open dropdownlist when the user clicks on the arrow on the right, not anywhere else...
        if (m.Msg == WM_LBUTTONDOWN || m.Msg == 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);
    }

Upvotes: 1

Related Questions