Charlie
Charlie

Reputation: 39

automatically click a button in a different form in vb.net

I have 2 forms in my VB application, I am populating a textbox with some text which is working fine but i then want to automatically click a button in another form and run the actions for that button click

i have this so far:

Form1.TextBox5.Text = "C:\folder\file1.csv"
Form1.Button8.PerformClick()

but its not clicking the button and performing the actions for Button8 on Form1

How can i make my other form click Button8 on Form1 and run its actions/events?

Upvotes: 0

Views: 9825

Answers (1)

scottyeatscode
scottyeatscode

Reputation: 606

You can do it like this. The forms will be different from yours because I coded it up as an example. Basically I have added a public method that allows another class to call PerformClick on its button. I believe that's what you were asking for.

' Form1 has two buttons, one for showing the Form2 object and another for performing the click on Form2.Button1
Public Class Form1
    Private form2 As Form2
    Private Sub ShowFormButton_Click(sender As System.Object, e As System.EventArgs) Handles ShowFormButton.Click
        form2 = New Form2()
        form2.Show()
    End Sub

    Private Sub PerformClickButton_Click(sender As System.Object, e As System.EventArgs) Handles PerformClickButton.Click
        If form2 IsNot Nothing Then
            form2.PerformClick()
        End If
    End Sub
End Class

' Form2 has a button and a textbox
Public Class Form2
    Public Sub PerformClick()
        Button1.PerformClick()
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        TextBox1.Text &= "Clicked! "
    End Sub
End Class

Upvotes: 1

Related Questions