Ashish Rathore
Ashish Rathore

Reputation: 2696

How to detect if tab key is pressed on listbox leave

I am developing an windows application. In that application i have a list box control on one form.Now i need to detect if user presses tab key or Shift+tab key.how can i detect this on list box leave event.

Upvotes: 0

Views: 1519

Answers (1)

Cosmin Ionascu
Cosmin Ionascu

Reputation: 7578

You need to get the KeyEventArgs to detect what key has been pressed. But assuming that when a user presses Tab or Shift+Tab the control loses focus you can just listen on the OnKeyDown or OnKeyPress and check if the pressed items are Tab or Shift+Tab


    private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    { 
        //Check for Tab key
        if (e.KeyCode == Keys.Tab)
        {
           //Do something
        }
        //Check for the Shift Key as well
        if (Control.ModifierKeys == Keys.Shift && e.KeyCode == Keys.Tab) {
            //Other stuff to do
        }
    }

Upvotes: 2

Related Questions