Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide DropDownList in ComboBox

How to hide DropDownList in ComboBox?

I want use ComboBox only for displaying text.
This control look nice, for me is better than TextBox plus Button.
So control must be enabled, but without any items.
When user click arrow (or alt + down key) DropDownList should'n show, because ill select value from custom DataGridView in order to fill back text in ComboBox.

Edit. Alternative solution is set DropDownHeight to 1, with show only 1 pixel line after clicking control.

Edit. Real solution. Answer below

like image 693
revelvice Avatar asked Feb 23 '26 06:02

revelvice


2 Answers

You can intercept the messages that cause the box to drop-down, in a subclass. The following snippet defines a control NoDropDownBox, that ignores the mouse clicks that result in a drop-down of the combo box:

public class NoDropDownBox : ComboBox
{
    public override bool PreProcessMessage(ref Message msg)
    {
        int WM_SYSKEYDOWN = 0x104;

        bool handled = false;

        if (msg.Msg == WM_SYSKEYDOWN)
        {
            Keys keyCode = (Keys)msg.WParam & Keys.KeyCode;

            switch (keyCode)
            {
                case Keys.Down:
                    handled = true;
                    break;
            }
        }

        if(false==handled)
            handled = base.PreProcessMessage(ref msg);

        return handled;
    }

    protected override void WndProc(ref Message m)
    {            
        switch(m.Msg)
        {
            case 0x201:
            case 0x203:
                break;

            default:
                base.WndProc(ref m);
                break;
        }
    }
}
like image 177
Edwin Groenendaal Avatar answered Feb 25 '26 23:02

Edwin Groenendaal


You will have less trouble and create a better end result by simply creating a usercontrol with a textbox and button that is styled in the way you want. If you figure out a way to remove the functionality of the combobox, all you're really doing is creating unneeded complexity.

like image 37
Paul Avatar answered Feb 25 '26 21:02

Paul