if (someCondition == true) { myButton.PerformClick(); }
private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { myButton.PerformClick(); } }
private void otherFormButton_Click(object sender, EventArgs e) { MainForm mainForm = Application.OpenForms.OfTypeIn this example, we have two forms: "MainForm" and "OtherForm". When the user clicks a button on "OtherForm", we want to simulate a click on a button control ("myButton") on the "MainForm". To do this, we first retrieve a reference to the "MainForm" using Application.OpenForms.OfType().FirstOrDefault(); if (mainForm != null) { mainForm.myButton.PerformClick(); } }