Catch an event from another form
-
Hello Everyone, sorry to bother you, this can be a stupid request but I am really stuck up. I have a windows form (let's call it Form1) which needs to be able to catch an event from another form (let's call it Form2). Form2 is launched from within a method in Form1. Form2 has just two buttons that can be clicked by the user, and I need to catch those events from Form1. Is it possible? and how can I do it without much trouble? ;-) Thanks in advance and sorry for my bad english. ~~~ From Milano to The Hague, easy as it goes ~~~
-
Hello Everyone, sorry to bother you, this can be a stupid request but I am really stuck up. I have a windows form (let's call it Form1) which needs to be able to catch an event from another form (let's call it Form2). Form2 is launched from within a method in Form1. Form2 has just two buttons that can be clicked by the user, and I need to catch those events from Form1. Is it possible? and how can I do it without much trouble? ;-) Thanks in advance and sorry for my bad english. ~~~ From Milano to The Hague, easy as it goes ~~~
The 'dirty' method would be to make the two buttons (the fields) public. Then Form1 could bind the event directly:
//in Form1
Form2 f = new Form2();
f.button1.Click += ...;
f.button2.Click += ...;Better would be to make public event in Form2 and letting Form1 catch those instead:
//in Form2
public event EventHandler Button1Clicked;
public event EventHandler Button2Clicked;//button1 event handler
private void button1_Click(object sender, EventArgs e) {
if (Button1Clicked != null)
Button1Clicked(this, EventArgs.Empty);
}//in Form1
Form2 f = new Form2();
f.Button1Clicked += ...;
f.Button2Clicked += ...; -
The 'dirty' method would be to make the two buttons (the fields) public. Then Form1 could bind the event directly:
//in Form1
Form2 f = new Form2();
f.button1.Click += ...;
f.button2.Click += ...;Better would be to make public event in Form2 and letting Form1 catch those instead:
//in Form2
public event EventHandler Button1Clicked;
public event EventHandler Button2Clicked;//button1 event handler
private void button1_Click(object sender, EventArgs e) {
if (Button1Clicked != null)
Button1Clicked(this, EventArgs.Empty);
}//in Form1
Form2 f = new Form2();
f.Button1Clicked += ...;
f.Button2Clicked += ...; -
sorry to bother you again, but even if it works in C# i can't get it working in VB.NET. Any ideas if there's some other way to do this (and in VB)? Thanks again ~~~ From Milano to The Hague, easy as it goes ~~~
This code works in Visual Studio 2003. Form1 has a ListBox on it. Form2 has two command buttons named btn1frm2 and btn2frm2. Programatically create two command buttons on form1 and set them equal to the command buttons on form2. Now you can capture the events from the buttons on form2 in the event handlers for the buttons you programatically created on form1 as follows:
Private WithEvents b1f2 As Button Private WithEvents b2f2 As Button Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim form2 As New Form2 b1f2 = form2.btn1frm2 b2f2 = form2.btn2frm2 AddHandler b1f2.Click, AddressOf b1f2_click AddHandler b2f2.Click, AddressOf b2f2_click form2.Show() End Sub Private Sub b1f2_click(ByVal sender As System.Object, ByVal e As System.EventArgs) ListBox1.Items.Add("1") End Sub Private Sub b2f2_click(ByVal sender As System.Object, ByVal e As System.EventArgs) ListBox1.Items.Add("2") End Sub