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?

djv
  • 15,168
  • 7
  • 48
  • 72
Muhammad zubair
  • 251
  • 1
  • 3
  • 13
  • Is `frmUpdateDueDates` the starting form? – Idle_Mind Dec 13 '21 at 15:11
  • @Idle_Mind The Starting form is frmIOMain – Muhammad zubair Dec 13 '21 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. – Idle_Mind Dec 13 '21 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. – Muhammad zubair Dec 13 '21 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? – Idle_Mind Dec 13 '21 at 15:33
  • I think he's showing that he's using a default form instance – djv Dec 13 '21 at 15:45
  • @Muhammadzubair "...but it is not working for me" can you elaborate please? – djv Dec 13 '21 at 15:51
  • @Idle_Mind yes it is opening and working fine but now i am trying to open that through frmIOMain directly. At the moment to open it i have to do the following Run the application frmIOMain loaded -> Press button --> frmUpdateDueDates (Loaded) --> Press the button --> frmUpdateDueDates so to reach to frmUpdateDueDates i have to do these steps i want kind of shortcut to display directly it on my frmIOMain load. when my main form load it load the frmUpdateDueDates with it. – Muhammad zubair Dec 13 '21 at 18:53
  • @djv please read my last comment i tried to explain it more hope it will clear your query. Please let me know if you know the solution – Muhammad zubair Dec 13 '21 at 18:54
  • @Muhammadzubair we're wondering what `frmUpdateDueDates (Loaded)` means. Are you just doing what's in the code i.e. `frmUpdateDueDates.cmdUpdate_Click(Nothing, Nothing)` to open it? Then you are not creating your own instance, rather using the default form instance, which is never recommended. – djv Dec 13 '21 at 18:58
  • @djv by load i mean open. frmUpdateDueDates.cmdUpdate_Click(Nothing, Nothing) this line was kind of try to open it through frmIOMain but it does not work me. i tried other solutions as well but none of them worked for me that is why i posted it here. Can you please write your solution which you are saying i.e. creation of the instance i tried but didn't worked for me that too – Muhammad zubair Dec 13 '21 at 19:08

1 Answers1

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
djv
  • 15,168
  • 7
  • 48
  • 72
  • Error at this line :- myFrmUpdateDueDates = New FrmUpdateDueDates() Argument not Specified for parameter 'db' of 'Public Sub New (db as Entities)' – Muhammad zubair Dec 13 '21 at 19:33
  • @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 – djv Dec 13 '21 at 19:40