如何从另一个表单触发按钮点击 VB Net

How to Trigger button click from another form VB Net

我总共有三个表单,我想触发表单 3 上的其中一个按钮,以便在加载表单 1 时自动触发

表格 1

Public Class frmIOMain 

    ' In This Form load I want to trigger the above mentioned button

    Private Sub IOMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        ' I want to Trigger the Above mentioned button here when my form is loaded
        ' But it is not working for me

    frmUpdateDueDates.cmdUpdate_Click(Nothing, Nothing)

    End Sub
End Class

表格 2

Public Class TestEquipmentManagement
    Public EquipementTable As New DataTable("EquipmentTable")
    Public EquiTypeSelection As String
    Public EquiManufacturerSelection As String
    Public EquiList_PK As New List(Of Integer)
    Dim targetEquipmentList As New List(Of Model.equipment)
    Private equipDB As Model.Entities = Nothing
    Public Shared viewManager As ViewManager
    
    Private equipment As New List(Of Model.equipment)
    'Dim WithEvents excNewPFM As New VBACom
    Public EquipCalTable As New DataTable("EquipCalTable")
    Public Sub New()
        Dim todayplusoneyear As Date
        todayplusoneyear = Date.Today
        todayplusoneyear = todayplusoneyear.AddYears(1)
    
        'Assign current db
        equipDB = frmIOMain.db
    End Sub 
End Class

表格 3

Public Class frmUpdateDueDates

    Private EquipmentUpdates As UpdateCalibrationsViewModel
    Private _success As Boolean = False
    Public Sub New(db As Entities)

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        EquipmentUpdates = New UpdateCalibrationsViewModel(db, New CAQ23(), False)
        'Add Handlers
        AddHandler EquipmentUpdates.OnProgressChanged, AddressOf progressChangedHandler
        AddHandler EquipmentUpdates.OnInfotextChanged, AddressOf infoTextChangedHandler

        prgUpdates.Maximum = EquipmentUpdates.intProgressMax
    End Sub
    Public Sub cmdUpdate_Click(sender As Object, e As EventArgs) Handles cmdUpdate.Click
        cmdUpdate.Enabled = False
        _success = EquipmentUpdates.startUpdating()
        cmdCancel.Text = "Close"
    End Sub
End Class

我希望 "cmdUpdate_Click" 加载表单 1 时触发表单 3 上的按钮

谁能告诉我该怎么做?

首先,创建一个表单实例,而不是使用其默认表单实例。跨表单调用点击处理程序不是一个好主意。处理程序可能会使用参数 sender As Object, e As EventArgs 并且在包含 class 之外,你不能假设你知道这一点。更好的做法是创建一个在表单内执行点击的方法,例如

Public Class frmUpdateDueDates

    Public Sub cmdUpdateClick()
        cmdUpdate.PerformClick()
    End Sub

    Private Sub cmdUpdate_Click(sender As Object, e As EventArgs) Handles cmdUpdate.Click
        cmdUpdate.Enabled = False
        _success = EquipmentUpdates.startUpdating()
        cmdCancel.Text = "Close"
    End Sub
End Class
Public Class frmIOMain 

    Private myFrmUpdateDueDates As frmUpdateDueDates

    Private Sub IOMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        myFrmUpdateDueDates = New FrmUpdateDueDates()
        myFrmUpdateDueDates.Show()
        'myFrmUpdateDueDates.cmdUpdate_Click(Nothing, Nothing)
        myFrmUpdateDueDates.cmdUpdateClick()
    End Sub
End Class

并且您可以将点击处理程序的访问修饰符改回私有

更好的方法是将工作放入单击处理程序调用的不同方法中。那么另一个表单甚至不需要知道按钮的存在。比如

Public Class frmUpdateDueDates

    Public Sub DoUpdating()
        cmdUpdate.Enabled = False
        _success = EquipmentUpdates.startUpdating()
        cmdCancel.Text = "Close"
    End Sub

    Private Sub cmdUpdate_Click(sender As Object, e As EventArgs) Handles cmdUpdate.Click
        DoUpdating()
    End Sub
End Class
Public Class frmIOMain 

    Private myFrmUpdateDueDates As frmUpdateDueDates

    Private Sub IOMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        myFrmUpdateDueDates = New FrmUpdateDueDates()
        myFrmUpdateDueDates.Show()
        'myFrmUpdateDueDates.cmdUpdate_Click(Nothing, Nothing)
        myFrmUpdateDueDates.DoUpdating()
    End Sub
End Class