在vb.net中自动单击不同形式的按钮

时间:2014-09-29 19:42:43

标签: vb.net

我的VB应用程序中有2个表单,我正在填充文本框,其中包含一些正常工作的文本,但我想自动单击另一个表单中的按钮并运行该按钮的操作单击

到目前为止我有这个:

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

但它没有点击按钮并在Button8

上执行Form1的操作

如何在Form1上单击Button8并运行其动作/事件?

1 个答案:

答案 0 :(得分:1)

你可以这样做。表格将与您的不同,因为我将其编码为一个例子。基本上我添加了一个公共方法,允许另一个类在其按钮上调用PerformClick。我相信你所要求的。

' 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