Can I disable keyboard input to a specific control?

20,147

Solution 1

Handling the KeyDown event is too late, but you can handle the PreviewKeyDown event and that should give you the behavior you are looking for:

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

Solution 2

Dear maciek, the only thig you need to do is using OnKeyDown event just do

private void txtInput_KeyDown(object sender, KeyEventArgs e)
    {
            e.Handled = true; // user can input
            e.Handled = false; // user cannot input
    }

Solution 3

KeyDown generally works for me if you do the following in it:

e.Handled = true;
e.SuppressKeyPress = true;

A more complete example with a practical application (disabling input from non-numeric characters): http://cccontrols.codeplex.com/SourceControl/changeset/view/34146#611536

John makes a good point though. Any reason you would want to disable interaction with the Control but not set Enabled = false?

Edit: I just noticed the WPF tag. Not so sure of my answer anymore since I'm a WPF hater ;-)

Solution 4

That is the purpose of the WebControl.Enabled = false; to prevent it from responding to user input.

edit: now that the question has changed, disabling the control is no longer a solution. However I think a control that responds to mouse clicks by not keyboard is buggy, not everyone prefers to use the mouse.

Share:
20,147
Maciek
Author by

Maciek

Updated on July 14, 2022

Comments

  • Maciek
    Maciek almost 2 years

    Is it possible to disable keyboard input to a control? For instance a ListView? How do I do that? I've tried overriding the KeyUp KeyDown events but apparently that was not the way?

    IsEnabled is a good solution, however I only wish to disable keyboard interaction and leave mouse interaction intact.

  • Maciek
    Maciek over 14 years
    can't access e.SuppressKeyPress :/
  • Nasser Hadjloo
    Nasser Hadjloo over 14 years
    I apply a minus point, cause this approach will disable the control,and that not the solution
  • Nasser Hadjloo
    Nasser Hadjloo over 14 years
    hey Maciek, youshouldn't use both of Handled code above, as I mentioned in comments you have to use on of those