Please help event handling

izeko

Member
Joined
Jun 20, 2007
Messages
8
Programming Experience
Beginner
Hi,
I have created a 2 new instances of a form which i have designed. This form has a submit button on it amongst other things. How can i create 2 different event handlers for the submit button on each form, as i want one form to do one thing and the other to do something different when the button is pressed.


Many thanks

Dave (uk)
 
Hi Dave,

If you want both forms to do something different at the same time when ONE button is clicked, then try this:

VB.NET:
Form1.SubmitButton_click
    Call Form2.SubmitButton_click
or
VB.NET:
Form1.SubmitButton_click
    Form2.SubmitButton.PerformClick()

On the other hand, if you just want the buttons to do different things when clicked individually, all you have to do is set up a button.click procedure in the code window for each form.

HTH
Brian
 
what about this

If i have a main form that creates an instance of form1, which has a button.
When the button is clicked it displays the msg. But it only works for the last form created.





Public Class Main

Private myform As Form1
Private WithEvents mybutton As Button
------------------------------------------------------------------------
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

myform = New Form1
mybutton = myform.Button1

myform.Show()

End Sub
-----------------------------------------------------------------------
Private Sub buttonClick(ByVal Sender As Object, ByVal e As EventArgs) Handles mybutton.Click

MsgBox("It works")

End Sub
End Class
 
Let me make sure i properly understand what it is you're trying to do.
You have two (or more) forms, both of which have a button on them. You want both buttons to perform the same actions when clicked? If this is the case, then what you have to do is create the event handler for them in a code module (which you can add from the 'Add Item' option in the toolbar or file menu). The code you would put in would look something like this:

VB.NET:
Public Sub button_Click(ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Form1.button.Click, Form2.button.Click
    messagebox.show("It works!")
End Sub

Note that the 'Handles Form1.button.Click, Form2.button.Click' is what tells VB that this code should be executed when either of the buttons is clicked.

I'm not at home just now, so I haven't had the chance to test this code but it should be OK. If not, you can create a procedure in the code module which will be called from the click event of any of the buttons you want, something like this:

Code for module:
VB.NET:
Public sub ButtonClicked()
    MessageBox.Show("It Works!")
End Sub

Code for button on form 1 (and any other forms as well):
VB.NET:
Public Sub button1_Click(ByVal sender as System.Object, ByVal e as System.EventArgs) Handles button1.Click
    Call ButtonClicked()
End Sub

You can get a bit more detail on this in the BOL section for How to: Connect Multiple Events to a Single Event Handler in Windows Forms

Let us know how you get on.

Brian
 
Back
Top