简体   繁体   English

VB.Net调用此函数的最佳方法?

[英]VB.Net Best way to call this function?

Using the code below, what is the best way to call the second function (Checkbox1.CheckedChanged)? 使用下面的代码,调用第二个函数(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. 我尝试使用(sender,e),但是每次程序完成安装后,它都会连续调用App2.msi,并在尝试重复安装App2.msi时将程序置于连续循环中。 ()”,但我的代码无法编译。

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. 我对VB还是很陌生,所以我不确定是否应该以不同的方式调用msi,或者我是否还不了解该如何调用这样的名称。 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

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. 如果该方法包含要在某些情况下手动调用的代码,请将其移至 CheckBox1_CheckedChanged和您的其他代码调用的单独方法。

Furthermore, the comment is right: if you change CheckBox1.Checked , it calls the event handler again – you get an infinite loop. 此外,该注释是正确的:如果更改CheckBox1.Checked ,它将再次调用事件处理程序–您将获得无限循环。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM