Click here to Skip to main content
15,902,893 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i have two forms ( Form1 & Form2 )
I want to call a button from Form1 when clicking a button on Form2
Thanks for the replay,,
Posted
Comments
Snesh Prajapati 18-Aug-14 7:35am    
Help me to understand clearly...you are having two win forms...on clicking on first button on first form...you want to display second form? ...or what do you mean by "call a button"....Is that mean firing on click event of second button in second form ?
Thanks7872 18-Aug-14 7:45am    
Why?
Sergey Alexandrovich Kryukov 18-Aug-14 12:20pm    
Indeed, why? This is the indication of poor design, most likely.
—SA
Peter M. Adeeb 18-Aug-14 8:00am    
i mean the second one
Sergey Alexandrovich Kryukov 18-Aug-14 12:20pm    
There is no such concept as "call a button". You need to have one method called by event handler of either button.
But the question indicates that you may have poor design, should be reviewed.
—SA

Hi Peter,

by calling Button in Form1 when clicking a button on Form2 I understand that you want to invoke the event click on the button in the form1 right?
If that is the case you must first go to the source code of the Form 1 and make the method button1_click(...) public

e.g.:

C#
public void button1_Click(object sender, EventArgs e)
{

}


Then in the form2 button click event you can call the method in the form1

C#
public void button1_Click(object sender, EventArgs e)
{
    Form1 frm = new Form1();
    frm.button1_Click(sender, e);
}


Hope that helps.
 
Share this answer
 
Comments
Peter M. Adeeb 18-Aug-14 9:46am    
an error happened
(( alarm_test.Form1.button1_Click(object, System.EventArgs)' is inaccessible due to its protection level ))
note: button1 in form1 modifier in public
there are many ways to archieve this. Maybe the easiest one is making the form1 button1_click public and then just in form2 button_click call form1.button1_Click(null, null);
 
Share this answer
 
 
Share this answer
 
You could create an interface that must be implemented in the Form1 and use it in Form2 to communicate between two forms like in the example below.
C#
public interface ILinkedForm
{
   void ClickMyButton(EventArgs e);
}

public class Form1 : Form, ILinkedForm
{
    //...
    //
    public void ClickMyButton(EventArgs e)
    {
          button1_Click(this, e);
    }
     //...
}
//...
//
public class Form2 : Form
{
     public ILinkedForm LinkedForm { get; set;}
     //...
     public void button1_Click(object sender, EventArgs e)
     {
       //...
       this.LinkedForm.ClickMyButton(e); //Invoke the button click from Form1! 
     } 
}
//
static void UseForms()
{
//...
Form1 form1 = new Form1();
//...
Form2 form2 = new Form2();
form2.LinkedForm = form1; // Must be set the link between the forms!
//

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900