How to use buttons in c#

Posted on 2009-02-19
Last Modified: 2013-12-14
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.
Question by:kyrenator
    LVL 85

    Accepted Solution

    "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.
    LVL 7

    Expert Comment

    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

    LVL 7

    Expert Comment

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

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
    This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
    The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now