Click event of a Button on Click event of another button in C#

The following code example demonstrates how to call the Click event of a Button on Click event of another button. The following code assumes that two Button and one TextBox controls have been instantiated on a form. On the click of the button1, we will generate the click event of the button2 if the the value of the textbox1 is numeric.

private void button1_Click(object sender, EventArgs e)
   {
      int number = 0;
      // If value of the textbox1 is a number, click Button2. 
      if (int.TryParse(TextBox1.Text,out number))
        {
            button2.PerformClick();
            MessageBox.Show("button2 was clicked ");
        }
       else
        {
          MessageBox.Show("button2 was NOT clicked");
         }
}

We used the int.TryParse method in the above program. This method is good for parsing strings into numbers, when the string may not be numeric, the method returns true if it succeeds, and false if it doesn’t.