1

Consider i am having two forms, form1 and form2

How can i click,mouse over(any events) a specific button on another form using coding in vb.net?

1
  • What are you trying to achieve? Are you just trying to execute some code in Form1 from Form2? Commented Apr 21, 2012 at 7:21

2 Answers 2

3

I'm assuming that Form1 launches Form2, since there's not a whole lot of information in the description.

When Form1 launches, there are two buttons: "button1" and "Launch Form 2" (forgot to change text on button1, sorry. :(

form1

When you click "Launch Form 2", Form2 pops up:

form2

Clicking the "button1" on Form1, a message box originating from Form1 pops up saying:

form1Message

Clicking the "button1" on Form2, a message box ALSO originating from Form1 pops up saying:

form2Message

Here's the code:

Form1

Public Class Form1

    Private WithEvents frm2 As New Form2

    Private Sub Form1Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form1Button.Click
        RunSomeCode("Called from form 1!")
    End Sub

    Public Sub RunSomeCode(ByVal message As String)
        MessageBox.Show(message)
    End Sub

    Private Sub Form1LaunchForm2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form1LaunchForm2Button.Click
        frm2.Activate()
        frm2.Show()
    End Sub

    Private Sub frm2_SimulateForm1ButtonClick() Handles frm2.SimulateForm1ButtonClick
        RunSomeCode("Called from form 2!")
    End Sub
End Class

Form2

Public Class Form2

    Public Event SimulateForm1ButtonClick()

    Private Sub Form2Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Form2Button.Click
        RaiseEvent SimulateForm1ButtonClick()
    End Sub
End Class

How it works

Form 2 has a public event called "SimulateForm1ButtonClick". That event can be raised whenever you want, from any code block. I just decided to raise it when I click the button on the form.

Form 1 has an instance of Form2 WithEvents. It's very important that you use the WithEvents keyword, or that public event in Form2 won't show up. :(

Form 1 has a sub that handles the "SimulateForm1ButtonClick" that is raised when Form2 clicks its button.

Now, here's another important detail: The code executed when button1 is clicked on Form1 is actually in a private sub called RunSomeCode(). This is important, because it makes the code accessible from any other part of Form1, namely the part that handles Form2's event.

I hope that helps you out a little bit. I'm not sure exactly what you were asking. :/

Code: http://darin.hoover.fm/code/dl/FormsSandbox.zip

Sign up to request clarification or add additional context in comments.

Comments

2

If you are trying to fire the event, just use Form2.Button1.PerformClick() assuming that the button on form 2 is called 'button1'.

2 Comments

I would prefer to put the code from the click-method in a separate method which is public. So you can call this method in both forms.
Public Sub Button1_Click Form2.Button1.PerformClick() End Sub

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.