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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26107612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:17:42  来源:igfitidea点击:

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

vb.net

提问by Charlie

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

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

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 Button8on Form1

但它没有单击按钮并执行Button8onForm1

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

如何让我的其他表单单击 Form1 上的 Button8 并运行其操作/事件?

回答by scottyeatscode

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.

你可以这样做。表格将与您的不同,因为我将其编码为示例。基本上,我添加了一个公共方法,允许另一个类在其按钮上调用 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