How to programmatically press the button?

    4 answers 4

    button.PerformClick ()?

      If you need to press a button in another application, then there are options:

      • emulate regular and hot keystrokes using SendKeys calls;
      • use P / Invoke to find handles of buttons and send messages to SendMessage;
      • use the UI Automation API ( example ).

        button1.PerformClick();


        Just do not do it in any way in the event of pressing the button, otherwise there will be an endless recursion of click calls!

           private void button1_Click(object sender, EventArgs e) { MessageBox.Show("Мир."); } private void button2_Click(object sender, EventArgs e) { button1.PerformClick(); }