user1074030
user1074030

Reputation: 93

C# Keyboard button used to trigger a method

I'm writing a Hangman program in C# and when I press a keyboard button I want the button on the form to be clicked. Where should I write this? In form1.load()?

Upvotes: 0

Views: 7983

Answers (5)

Fernando Silva
Fernando Silva

Reputation: 353

No, you should select the Form on which you want the event to be triggered, then go to the properties pane, select the event tab and go down to KeyPress event, double click it and add some code.

Normaly something like this would do what you want, just google the KeyChar value to determine the keyPress you want to control, you can add more if statements:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)13)
        {
            Button1.PerformClick();
        }

    //Other if statements if you want to keep an eye out for other keyPresses
}

[edit] I just remembered you might be also considering the shortcuts, in wich case the Button1.Text property should be marked &Button1, this way the "B" would be underlined and accept the alt+B shortcut to execute the button click event. The & symbol is set before the letter you want for the shortcut, make sure you dont use the same letter for various buttons.

Upvotes: 3

Cody Gray
Cody Gray

Reputation: 244722

You wouldn't write the code in Form.Load() if you want it to happen in response to a keyboard event. That event occurs (and the code inside of it is executed) when your form first loads (appears on screen).

How about handling the KeyPress event and writing the code in that method, instead? Your form has one of those events, too.

Sample code:

private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
    Button1.PerformClick();
}

The PerformClick method will generate a Click event for a Button control. You can handle that Click event in a similar way:

private void Button1_Click(object sender, EventArgs e)
{
    // do something in response to the button being clicked
    // ...

    MessageBox.Show("Button clicked!");
}

If this event-handling stuff is confusing to you, make sure that you pick up a good book on programming in C# and/or the .NET Framework so that you learn it well. It's very important and not something to skip!

Upvotes: 2

aleroot
aleroot

Reputation: 72636

You have to enable KeyPreview property on the form, then you have to implements the KeyPress event directly on the form :

Form1.KeyPress +=new KeyPressEventHandler(Form1_KeyPress);

private void Form1_KeyPress(object sender, KeyPressEventArgs e) {
        if(e.KeyCode == someKey) {
button1.performclick();
 }

}

Upvotes: 1

Tigran
Tigran

Reputation: 62248

You need to subscribe to the events of interest and process them after. But before you need to read and study how to do that. It's a not a difficult issue in C#.

http://msdn.microsoft.com/en-us/library/ms171534.aspx

One time subscribed to the events create a function that you can call from button click and from keydown.

Upvotes: 0

MahanGM
MahanGM

Reputation: 2382

private void Form_KeyDown(object sender, KeyEventArgs e)
{
  if(e.KeyCode == Keys.Left)
  {
    // Do your hang job
    Button_Click(sender, EventArgs.Empty);
  }
}

Upvotes: 0

Related Questions