• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

How to use buttons in c#

Hello. Can you plz tell me how can i use buttons of the keyboard in a program with c#?
For exampe if the user click a button it will automaticly use the alt+ctrl+del command.
0
kyrenator
Asked:
kyrenator
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"For exampe if the user click a button it will automaticly use the alt+ctrl+del command."

Bad example...and hopefully not really what you want.  You can't programmatically simulate the pressing of Ctrl-Alt-Del, otherwise known as the SAS (Secure Attention Sequence)...

You can simulate the pressing of other keys using SendKeys.Send(), or via the SendInput() or SendMessage() APIs though.
0
 
HalfAsleepCommented:
If you have a form, you have to set the KeyPreview property to true, that way, you can add a keypress/keyDown event handler for the form that will be called before the framework looks for components to delegate the event to.

The code I have supplied, is for a user dialogue form with a yes button and a no button.  If the user presses escape, i treat it as if he mouse clicks the no button.  If he presses enter, I treat it as if he mouse clicks the Yes button.

The handled and suppress properties are set to tell the framework that it does not need to keep looking for components that may want to know about keypress events.

this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.FormUserDialogue_KeyDown);
 
private void FormUserDialogue_KeyDown(object sender, KeyEventArgs e)
{
   e.Handled = true;
   e.SuppressKeyPress = true;
   if (e.KeyCode == Keys.Escape)
   {
      this.DialogResult = DialogResult.Cancel;
   }
   else if( e.KeyCode == Keys.Enter )
   {
      this.DialogResult = DialogResult.OK;
   }
}

Open in new window

0
 
HalfAsleepCommented:
Heh, I reread the question, and if you want to simulate keypresses, you can use the suggestions of Idle_Mind.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now