2

我的目的是在选择新的单选按钮时清除计算的标签。目前,我正在使用 7 个单独的点击事件调用相同的子过程,如下所示:

' the following 7 subprocedures clear totals boxes if any radio button is changed
Private Sub rbCappuccino_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbCappuccino.CheckedChanged
    Call ClearLabels()
End Sub

Private Sub rbChocolate_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbChocolate.CheckedChanged
    Call ClearLabels()
End Sub

Private Sub rbFilled_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbFilled.CheckedChanged
    Call ClearLabels()
End Sub

Private Sub rbGlazed_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbGlazed.CheckedChanged
    Call ClearLabels()
End Sub

Private Sub rbNone_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbNone.CheckedChanged
    Call ClearLabels()
End Sub

Private Sub rbRegular_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbRegular.CheckedChanged
    Call ClearLabels()
End Sub

Private Sub rbSugar_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbSugar.CheckedChanged
    Call ClearLabels()
End Sub

我很好奇是否有办法整合这些事件,因为它们都调用了完全相同的子过程。任何意见,将不胜感激。谢谢!

4

1 回答 1

5

是的,您可以将多个事件附加到单个事件处理程序:

Private Sub CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles rbCappuccino.CheckedChanged, rbChocolate.CheckedChanged, rbFilled.CheckedChanged, rbGlazed.CheckedChanged, rbNone.CheckedChanged, rbRegular.CheckedChanged, rbSugar.CheckedChanged
    Call ClearLabels()
End Sub

在这样的代码中,您甚至可以检测到哪个 CheckBox 调用了事件以作用于各个控件:

If CType(sender, CheckBox).Text = "Cup of Java" ....
于 2013-08-01T01:44:42.997 回答