tags:

views:

487

answers:

3

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?

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

With this you can cancel all KB actions on ur ComboBox.
Asssemptions: WinForms

nils_gate
By that same logic, I contend that your example is too wide in scope. Notice that the OP says "I block the user to select **some items**"
Cerebrus
My pardon for the comment then :)
nils_gate
A: 

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. ^ ^

Cerebrus
If you have an item with text, say "xyz", on pressing 'x' it will be selected. The above code will not handle that.
nils_gate
My example is illustrative. I leave the exclusion of certain keyboard events to the OP.
Cerebrus
I agree Cerebrus. I saw the method, I decided how to handle it in my case
A: 

Would please share your decision with me, i really m looking for such decision.

I appreciate your fast reply!

emwedish