Home > Software design >  How to Trigger button click from another form VB Net
How to Trigger button click from another form VB Net

Time:12-21

I have three forms in total and i want to trigger one of the button on form 3 to be triggered automatically when form 1 loaded

Form 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

Form 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

Form 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

I want "cmdUpdate_Click" Button which is on form 3 to be triggered when my form 1 is loaded

Can Anyone tell me how i can do that?

CodePudding user response:

Firstly, create an instance of the form, instead of using its default form instance. Calling a click handler across forms isn't a good idea. The handler may use the arguments sender As Object, e As EventArgs and from outside of the containing class, you can't assume you know that. Better practice would be to create a method which performs the click within the form, such as

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

And you can change the access modifier of the click handler back to Private

Even better would be to put the work into a different method which the click handler calls. Then the other form doesn't even need to know the button exists. Such as

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
  • Related