0

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?

4
  • Hint: You know that you can have more than one instance of a Form1 class, right? Commented Sep 29, 2014 at 19:59
  • What is the connection between the two forms: does Form1 make the second Form show? Commented Sep 29, 2014 at 20:01
  • the code above i have on my first form (Form = produce_bills.vb) so Form1.vb is the second form Commented Sep 29, 2014 at 20:10
  • its a bad idea to have one form clicking the controls on another. Soon you will be asking how to tell when it was clicked from code or by the user. Create a Public Sub with the relevant code and both the local click event and the other form can call that. Even that is pretty greasy. Commented Sep 30, 2014 at 0:26

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

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.