Joeri
Joeri

Reputation: 361

Combobox cancel dropdown

I've got a combobox that opens a new form window with a datagridview, and I want the users to choose the items through that datagridview rather than through the combobox. I've got this code to achieve that:

    private void comboBox1_DropDown(object sender, EventArgs e)
    {
        valSel.incBox = (ComboBox)sender;            
        valSel.Show();
        if (this.comboBox1.DroppedDown)
        {
            MessageBox.Show("test");
            SendMessage(this.comboBox1.Handle, CB_SHOWDROPDOWN, 0, 0);
        }
    }

As you see I'm also trying to hide the dropdown of the combobox but it isn't working. I assume it's because the combobox hasn't actually "dropped down" yet, so that part of the code is never run. Is there an event or something I can cell when the combobox has fully "dropped down" so i can send the message to close it again?

Upvotes: 0

Views: 4240

Answers (3)

Usman Ali
Usman Ali

Reputation: 1

1) create a KeyPress event on ComboBox from the properties. 2) write code

private void cmbClientId_KeyPress(object sender, KeyPressEventArgs e) { ((ComboBox)sender).DroppedDown = false; }

Upvotes: 0

Eduard
Eduard

Reputation: 21

In comboBox1.Enter set the focus to a different control if condition is met.

 private void comboBox1_Enter(object sender, EventArgs e)
    {
        if (comboBox1.Items.Count < 1)
        {
            comboBox1.DroppedDown = false;
            comboBox2.Focus();
            MessageBox.Show("Select a list first");
            comboBox2.DroppedDown = true;
        }
    }

Upvotes: 0

Mario
Mario

Reputation: 36497

You should be able to simply set the height of the ComboBox to something really small. Last time I looked at it, this determined the height of the popup part (the actual height of the control is determined by the UI/font size).

The more elegant way, however, would be using a custom control that just mimics the appearance of dropdown boxes (I'm rather sure that can be done some easy way).

Upvotes: 2

Related Questions