none
Can I "Click" a WinForms button programmatically? RRS feed

  • Question

  • Is it possible to simulate a "Click" event to a WinForms button control programaticly? I am trying to tie a function key, such as <F8>, to "Click" an existing button control, such as a "Save" button.
    • Edited by Bill2010 Tuesday, August 4, 2009 5:54 PM
    Friday, July 31, 2009 11:16 PM

Answers

  • button1.PerformClick();
    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, July 31, 2009 11:26 PM
    Moderator
  • Hi Bill,

     

    Button.PerformClick can generates a Click event for a button, as Rudedog2 has suggested.   For detailed code example, you can refer to the following code snippet to click button1 when F8 is pressed in a Windows Forms application:

     

    Override the ProcessCmdKey method

    =======================================================

            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

            {

                if (keyData == Keys.F8)

                {

                    button1.PerformClick();

             

                    // Or directly calling the button's click event handler,

                    // unless the handler is defined by annonymous methods.

                    //button1_Click(button1, EventArgs.Empty);

                }

                return base.ProcessCmdKey(ref msg, keyData);

            }

    =======================================================

     

    Have a nice day, Bill and Rudedog2!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bill2010 Monday, August 3, 2009 5:05 PM
    Monday, August 3, 2009 7:55 AM
    Moderator

All replies

  • button1.PerformClick();
    Mark the best replies as answers. "Fooling computers since 1971."
    Friday, July 31, 2009 11:26 PM
    Moderator
  • Hi Bill,

     

    Button.PerformClick can generates a Click event for a button, as Rudedog2 has suggested.   For detailed code example, you can refer to the following code snippet to click button1 when F8 is pressed in a Windows Forms application:

     

    Override the ProcessCmdKey method

    =======================================================

            protected override bool ProcessCmdKey(ref Message msg, Keys keyData)

            {

                if (keyData == Keys.F8)

                {

                    button1.PerformClick();

             

                    // Or directly calling the button's click event handler,

                    // unless the handler is defined by annonymous methods.

                    //button1_Click(button1, EventArgs.Empty);

                }

                return base.ProcessCmdKey(ref msg, keyData);

            }

    =======================================================

     

    Have a nice day, Bill and Rudedog2!

     

     

    Best Regards,
    Lingzhi Sun

    MSDN Subscriber Support in Forum

    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bill2010 Monday, August 3, 2009 5:05 PM
    Monday, August 3, 2009 7:55 AM
    Moderator