Manual implementation of the ComboBox drop-down list

I wrote a Listbox, it looks like this:

custom Listbox

Now you need to make a Combobox, but I can't understand one nuance. If all the previous controls had a specific specified size (Checkbox, Label, Textbox, Button), then when opening the Combobox visually, its size increases to a certain maximum specified in the properties.

How to make a "drop-down menu" (in fact, a combobox is a stylized button + a drop-down on top total and beyond the borders of the listbox window)? Maybe you need to make a child form, and catch the loss of focus or the selection of an element?

No text input is expected. Only the selection of one of the presented elements. Also, I don't like the Dialog form option due to the loss of focus of the parent form.

Author: Nicolas Chabanovsky, 2015-11-09

3 answers

The pop-up list is a full-fledged top-level window. This window is not a child window, and the dimensions of the original control do not change.

To solve the problem of losing focus, use Form.ShowWithoutActivation:

protected override bool ShowWithoutActivation
{
    get { return true; }
}

P. S. You are going the inefficient way. If you need a complex stylization of controls and at the same time not killed in zero usability and accessibility (there is more logic in standard controls than you think), then look in the direction of WPF. There it's styling is supported out of the box and not requires a bicycle rewrite of all controls from scratch.

 1
Author: Kyubey, 2015-11-09 15:21:09

If I understand correctly, you need a combobox, which becomes similar to the listbox shown in the question, that is, without an input field.

Try using a standard ComboBox by defining two event handlers:

comboBox.DropDown += (o, e) =>
{
    comboBox.Top -= comboBox.Height;
    comboBox.Hide();
};

comboBox.DropDownClosed += (o, e) =>
{
    comboBox.Top += comboBox.Height;
    comboBox.Show();
};

Select the appropriate value for the property DropDownHeight.

For further customization, set the property

comboBox.DrawMode = DrawMode.OwnerDrawFixed;

And define the event handler DrawItem.

At the same time, you can use all the available features of the combobox, whether it's data binding, AutoComplete, and others.

 0
Author: Alexander Petrov, 2015-11-09 17:33:26

Solved the issue using a separate form. To automatically hide the form when the "focus" is lost, use the following redefined method:

protected override void WndProc(ref Message m)
    {
        const UInt32 WM_NCACTIVATE = 0x0086;

        bool handled = false;
        if (m.Msg == WM_NCACTIVATE && m.WParam.ToInt32() == 0)
        {
            handled = true;
            Close();
        }

        if (!handled)
            base.WndProc(ref m);
    }

enter a description of the image here

 0
Author: Newbie127, 2015-11-17 12:11:36