2

I've got a "Home" screen with 9 buttons. All text properties for the buttons are set like: &Returns so that the R is underlined in the display.

So when I press R at home screen I want the Returns screen to load. This is what I have:

private void frmHome_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.R)
    {
        frmReturns R = new frmReturns(empid);
        R.Show();
        this.Hide();
        e.Handled = true;
    }
}

But that does absolutely nothing. Can someone please help?

Werner van den Heever
  • 745
  • 6
  • 17
  • 40
  • How does it behave? Does the event fire at all? Does it fire and the If condition doesn't pass? – dutzu Feb 21 '13 at 08:50
  • You should check all the other posts relating to this type of functionality. Like for example this one http://stackoverflow.com/questions/5048748/how-to-set-hotkeys-for-a-winform – dutzu Feb 21 '13 at 08:51
  • @dutzu doesn't even trigger the event. – Werner van den Heever Feb 21 '13 at 08:52
  • Try hooking to a different event like Key_Down and make sure the form has Focus, otherwise the event will be triggered to another target. – dutzu Feb 21 '13 at 08:53

2 Answers2

3

You should set KeyPreview property of your form to true

this.KeyPreview = true;

otherwise the event of pressed key will be raised in some inner controls of form, not in the main form.

Also, your comparison is case sensitive. e.KeyChar == (char)Keys.R will only work for R but not for r.

if(char.ToUpper(e.KeyChar) == (char)Keys.R)
Dmitrii Dovgopolyi
  • 6,231
  • 2
  • 27
  • 44
0

I think this post is on a similar vein: CTRL + S to submit form and all inputs

I've done something similar to this and it does work. It might need more input, as you dont want to accidentally be typing hit r to have the page redirect somewhere before you have finished?

Community
  • 1
  • 1
Luke Duddridge
  • 4,285
  • 35
  • 50