user1114503
user1114503

Reputation: 149

VB.Net Best way to call this function?

Using the code below, what is the best way to call the second function (Checkbox1.CheckedChanged)? I tried using (sender, e) but then it continuously calls the App2.msi every time a program finishes installing, throwing the program into a continuous loop as it tries to install App2.msi repeatedly.. Tried it without a sender "Call CheckBox1_CheckedChanged()" but then my code won't compile.

I'm pretty new to VB so I'm not sure if I should be calling the msi's differently or if I'm just not knowledgeable enough yet to understand how to call something like this. If anyone needs anymore details, please let me know! THANK YOU!

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim p1 As New Process
    p1 = System.Diagnostics.Process.Start("App1.msi")
    p1.WaitForExit()
    CheckBox2.Checked = True
    Label2.Visible = True

Call CheckBox1_CheckedChanged()

End Sub

Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged

End Sub


Private Sub Label2_Click(sender As Object, e As EventArgs) Handles Label2.Click

End Sub

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    Dim p1 As New Process
    p1 = System.Diagnostics.Process.Start("App2.msi")
    p1.WaitForExit()
    CheckBox1.Checked = True
    Label3.Visible = True
End Sub

Private Sub Label3_Click(sender As Object, e As EventArgs) Handles Label3.Click

End Sub
End Class

Upvotes: 0

Views: 1225

Answers (2)

Ryan Gunn
Ryan Gunn

Reputation: 1169

Try this below

Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
    Dim oSender as CheckBox = DirectCast(sender,CheckBox)
    If oSender.Checked then
        Dim p1 As New Process
        p1 = System.Diagnostics.Process.Start("App2.msi")
        p1.WaitForExit()
        Label3.Visible = True
    End If
End Sub

Upvotes: 1

Konrad Rudolph
Konrad Rudolph

Reputation: 545995

The correct way is: don’t call it at all! It’s an event handler, it’s not supposed to be called by you.

If the method contains code that you want to call manually in some circumstances, move it to a separate method that is called by CheckBox1_CheckedChanged and your other code.

Furthermore, the comment is right: if you change CheckBox1.Checked, it calls the event handler again – you get an infinite loop.

Upvotes: 3

Related Questions