Cornel
Cornel

Reputation:

Preventing comboBox keyboard selection

As you now the user can select an item from the comboBox by keyboard directly. By mouse I block the user to select some items depending on the behind object state. What's the best solution to stop this when the user uses the keyboard?

Upvotes: 0

Views: 3510

Answers (2)

NileshChauhan
NileshChauhan

Reputation: 5569

    private void comboBox1_KeyDown(object sender, KeyEventArgs e)
    {
        e.Handled = true;
    }

With this you can cancel all keyboard actions on your ComboBox.

Assumptions: WinForms

Upvotes: 0

Cerebrus
Cerebrus

Reputation: 25775

Putting aside the usability issues arising from this sort of requirement (many users are in the habit of using the keyboard and would find it non-intuitive), you could simply handle the KeyDown event and set KeyEventArgs.Cancel to True.

private void myCombo_KeyDown(object sender, KeyEventArgs e)
{
   // Cancel the event if Up or Down keys are pressed.
   if ((e.KeyCode == Keys.Down) || (e.KeyCode == Keys.Up))
     e.Handled = true;
}

Edit: Clarification before I get downvoted again - This is an example which illustrates the method. It is upto the OP to decide what keys he/she wants to disallow. ^ ^

Upvotes: 2

Related Questions