JackMini36
JackMini36

Reputation: 619

Disable all key functions of a ListBox

I have a listbox in one of my user controls, and if certain keys are pressed it acts accordingly. My problem is that there are already some keys that do specific functions for the listbox (eg. arrows move up and down, dynamic search etc.). What I need is to disable all of these and handle the listbox on my own. Any way I could achieve this?

Upvotes: 1

Views: 1268

Answers (2)

Mehrdad Kamelzadeh
Mehrdad Kamelzadeh

Reputation: 1784

If you want to disable all keys on your ListBox, you can use the following code

private void Listbox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        e.Handled = true;
    }

You can accept some especial keys if you want as well. For example if you want to accept just the Enter key

 private void ListBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
       If e.Keycode==keys.Enter
       {
         //do what you want
       }
       Else
       {
         e.Handled = true;
       }
     }

Upvotes: 0

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73502

Inside the Form or UserControl where you have the listbox specified add this code:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (msg.HWnd == yourListBox.Handle)
    {
        //Check the keyData and do your custom processing
        return true;//Say that you processed the key.
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

Upvotes: 2

Related Questions