Short Cuts for Toolbar Buttons


It is easy to use short cuts on a button by appending "&" before the text property of the button.

For Example:

btnLogin.text=&Login

and that way we can use short cut Alt+ L for the button named btnLogin. But same is not the case with the toolbar button. We need to write a event handler in KeyDown event of the form. Make sure the KeyPreview property of the Form is set to True.Go to forms event tab double click on the keydown event you will be provided with blank function like below.

private void form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
}

KeyEventArgs provides event data thus we can capture the key presses. Here is an exmaple how we capture

private void form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if(e.Alt && e.KeyCode == Keys.G)
{
do_job1();
}
else if(e.Alt && e.KeyCode == Keys.P)
{
do_job2();
}
else if(e.Alt && e.KeyCode == Keys.L)
{
do_job3();
}
}

In the above example we capture alt(using e.Alt) and whatever key the user enters(using e.KeyCode). And we compare it with the Keys. Keys.letter(what ever letter you have set for the text property of the button) for example In btnLogin.text=&Game we check for Keys.G.

Hope that helps.


Similar Articles