call the click event of a button
-
here's te deal i have a form that has 2 buttons on it. i need to call the buttons click event from a different form in order to run the event i would just put the code into the form_load event but the form has a duel purpose so i can't do this. so in VB.NET, how do we call the click event form a different form i have tried the performclick() and this doesn't seem to work . here is a sample of code
Private Sub EnterBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles EnterBtn.Click ' these next 2 lines remove the unwanted characters from the ' beginning and end of the card value in this case, the card ' values are 4 numbers delimited with a ; at the beginning ' and a ? at the end txtPassID.Text = txtPassID.Text.Remove(0, 1) txtPassID.Text = txtPassID.Text.Remove(4, 1) '********************************************************************** CIfrm.txtPassIdSearch.Text = txtPassID.Text CIfrm.SearchIDBtn.PerformClick()<-- not performing as it should CIfrm.ShowDialog(Me) Me.Close() End Sub
i have to different searches being performed depending on what form the user uses. any help would be great Help is great only if you ask correctly :) -
here's te deal i have a form that has 2 buttons on it. i need to call the buttons click event from a different form in order to run the event i would just put the code into the form_load event but the form has a duel purpose so i can't do this. so in VB.NET, how do we call the click event form a different form i have tried the performclick() and this doesn't seem to work . here is a sample of code
Private Sub EnterBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles EnterBtn.Click ' these next 2 lines remove the unwanted characters from the ' beginning and end of the card value in this case, the card ' values are 4 numbers delimited with a ; at the beginning ' and a ? at the end txtPassID.Text = txtPassID.Text.Remove(0, 1) txtPassID.Text = txtPassID.Text.Remove(4, 1) '********************************************************************** CIfrm.txtPassIdSearch.Text = txtPassID.Text CIfrm.SearchIDBtn.PerformClick()<-- not performing as it should CIfrm.ShowDialog(Me) Me.Close() End Sub
i have to different searches being performed depending on what form the user uses. any help would be great Help is great only if you ask correctly :)You need to make the event handler method for the button click on Form2 public and within it check what control it is handling:
Public Sub Search(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click If sender.GetType Is GetType(Form1) Then MsgBox("Button Called By Form 1") ElseIf sender Is GetType(Button) Then MsgBox("Button Called By Button") End If End Sub
Then from the other form you can call this method in the load handler:Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim form1Instance As New Form1 'Only if you don't have another instance form1Instance.Search(sender, e) End Sub
Hope this helps, although maybe you want to rethink your design a bit though and have a another class that performs the search that each form could access independently:Public Class Search Public Shared Sub DoSearch(ByVal sender As Object, ByVal e As EventArgs) If sender.GetType Is GetType(Form1) Then MsgBox("Button Called By Form 1") ElseIf sender Is GetType(Button) Then MsgBox("Button Called By Button") End If End Sub End Class
You could then call this from anywhere without even creating an instance of the Search class using:Public Sub Search(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click Search.DoSearch(sender, e) End Sub
Cheers Tom