1

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?

11
  • Is frmUpdateDueDates the starting form? Commented Dec 13, 2021 at 15:11
  • @Idle_Mind The Starting form is frmIOMain Commented Dec 13, 2021 at 15:12
  • Did you display frmUpdateDueDates by creating an instance with the New keyword, or did you display it with the default instance using its NAME directly? I'm guessing the first option... Update your post to show how frmUpdateDueDates is displayed. Commented Dec 13, 2021 at 15:13
  • @Idle_Mind i will update the post. Actually there are three forms in total 1) the frmIOMain 2) TestEquipmentManagement 3) frmUpdateDueDates When i click on one button it takes me to the Form 2 (TestEquipmentManagement) and when i click in one of the button it takes me to form 3 frmUpdateDueDates. Now what i want is i want the last button which is on frmUpdateDueDate (form) to be automatically loaded when the form 1 (frmIOMain) is loaded. Commented Dec 13, 2021 at 15:21
  • 1
    You said that frmIOMain is the starting form...but you're not showing HOW frmUpdateDueDates is being displayed. If you're trying to click on the button in that form, then it needs to be open already. How is it being displayed? Commented Dec 13, 2021 at 15:33

1 Answer 1

1

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

2 Comments

Error at this line :- myFrmUpdateDueDates = New FrmUpdateDueDates() Argument not Specified for parameter 'db' of 'Public Sub New (db as Entities)'
@Muhammadzubair Well, maybe that's the issue all along when you tried to use the default form instance. So you have a parameterized constructor, you must know how to pass the db. I certainly don't

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.