4 Replies - 2864 Views - Last Post: 17 December 2012 - 10:29 AM Rate Topic: -----

#1 dorel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-December 12

question about button

Posted 11 December 2012 - 07:48 AM

Hei every body
First sorry about my English
I want to know how I can to Press on a key on a keyboard and its was like I am click on a button in a Form
Example:
If I click on a button in a Form he open MessegeBox and show hello
I want if i click ESCAPE in a keyborad its was like I click on a button
Thanks helpers
If you can show me the code because I'm new in c#
sorry about my English
Is This A Good Question/Topic? 0
  • +

Replies To: question about button

#2 magius96  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 707
  • Joined: 15-April 09

Re: question about button

Posted 11 December 2012 - 08:33 AM

To answer your question I'm going to assume that you are building a windows application with WinForms, since you didn't otherwise specify.

First you'll need to know upon what control the user's control focus will be set when you want to catch and handle the event. For instance, if you were building a chat program, then the control of interest would be the textbox where they are typing their text. On that control of interest you'll want to put a KeyPress event. This will be triggered every time any button is pressed on the keyboard. In the code for that event, you'll then want to test the event argument to determine which button was pressed and handle it accordingly.

Please note, the KeyPress event must be added to each and every control that could have focus. I've seen a lot of new coders that try to add the event to the form itself then wonder why it's not working. If you are typing in a textbox that is located on the form, the KeyPress event of the text box is triggered, not the KeyPress event of the form.

private void Control_KeyPress(object sender, KeyPressEventArgs e)
{
    if(e.KeyChar == 'q' || e.KeyChar == 'Q')
    {
        // Do something here when the q key is pressed
    }
}


----NOTE----
My suggestion above will only catch the key by it's character code. It does not capture the key stroke by an enumeration.

This post has been edited by tlhIn`toq: 15 December 2012 - 07:10 PM
Reason for edit:: code tags added

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,354
  • Joined: 02-June 10

Re: question about button

Posted 15 December 2012 - 07:14 PM

Quote

Please note, the KeyPress event must be added to each and every control that could have focus.


No. Set the KeyPreview property of the form to true. Then use the same type of KeyPress event handler for the Form instead of for each control.


private void myChatForm_KeyPress(object sender, KeyPressEventArgs e)
{
    if(e.KeyChar == 'q' || e.KeyChar == 'Q')
    {
        // Do something here when the q key is pressed
    }
}

Was This Post Helpful? 1
  • +
  • -

#4 magius96  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 707
  • Joined: 15-April 09

Re: question about button

Posted 17 December 2012 - 10:05 AM

Hah! Years later and I'm still being schooled! Way to go tlhIn`toq!

I definitely like your solution and will keep it in mind the next time I need key controls on a form. Now just to figure out why I had never of that solution before...
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,354
  • Joined: 02-June 10

Re: question about button

Posted 17 December 2012 - 10:29 AM

We all run across different things based on need and experience. I'm only just now getting into coding for databases and consuming on-line services - Yet most 20 year olds do this in their sleep.

That's what I love about coding communities, we all learn from each other.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1