I need some help to connect two windows forms. What I need is that when the button is pressed in form 2, form one would activate certain method? I tried using events but I couldn't make it work.
3 Answers
public class Form1
{
private Form2 form2;
public Form1(Form2 frm2)
{
form2 = frm2;
form2.ButtonClicked += button_clicked;
}
public void button_clicked(object sender, EventArgs e)
{
frm1btn_click(null, null);
}
}
public class Form2
{
public event EventHandler ButtonClicked;
Public Form2()
{
}
public void frm2btn_click(object sender, EventArgs e)
{
if(ButtonClicked != null)
{
ButtonClicked(null, null);
}
}
}
public class main
{
public static void main(string[] args)
{
Form2 form2 = new Form2();
Form1 form1 = new Form1(form2);
}
}
This is the basic idea for your events. When the user clicks the button in form2, your event will be triggered. Make sure form1 is listening to the event (that's the += line).
Your main method should instantiate both forms.
Also be aware that this is not best practice. It's uncommon for forms to trigger events on one another, and you most likely need an underlying model schema.
This would also be more complete if there was code.
Instead of trying to handle Click event of button (that you can do it simply) its better to Call other form's method in Click event handler of button :
- You should have a reference of other form to call its method.
- Method of other form should be public.
Code In form 2:
//You should set form1Instance=... somewhere in code
Form1 form1Instance;
public void button1_Click(object sender, EventArgs e)
{
//....
form1Instance.Method1();
}
Code In Form1:
Public void Method1()
{
//...
}

- 120,393
- 18
- 203
- 398
-
This assumes Form1 is instantiated in Form2. Usually it happens the other way around. MainWindow form instantiates Options form, for example, and you want an event in Options to trigger a method in MainWindow. So maybe oppassum's answer is more pertinent. – A. Vieira Mar 11 '20 at 11:26
-
1@A.Vieira It assumes somewhere in `Form2` you have `form1Instance = new Form1();`. If you are interested in an extended answer, take a look at [this post](https://stackoverflow.com/a/38769212/3110834). – Reza Aghaei Mar 11 '20 at 12:28
-
Don't get confused with Form1 and Form2, the names should be vice versa :D – Reza Aghaei Mar 11 '20 at 12:42
You can use the concept of MVVM Light. The Messenger
component of MVVM Light easily allows to pass data between classes.
follow the below steps:
1) on click of button in form2 and write code like below:
// Sends a message with a Student object.
var student = new Student{ FirstName = "Gul", LastName = "Ershad" };
Messenger.Default.Send(student);
2) Receive the incoming message from form2 to form1 and write code like below:
// Registers for incoming Student messages.
Messenger.Default.Register<Student>(this, (student) =>
{
// Works with the Student object.
});
Read details in the below link. Its very powerful concept of message passing between classes.
[MVVM Light][1]

- 1,743
- 2
- 25
- 32