Jerry
Jerry

Reputation: 71

Visual Basic: is there a way to consolidate 7 click-events that call the same sub procedure?

My intention is to have calculated labels clear anytime a new radio button is selected. Currently, I'm doing that with 7 separate click-events calling the same sub procedure, like so:

' 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

I'm curious as to whether or not there is a way to consolidate these events given they all call the exact same sub procedure. Any advice would be appreciated. Thanks!

Upvotes: 2

Views: 277

Answers (1)

suff trek
suff trek

Reputation: 39777

Yes, you can attach multiple events to a single event handler:

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

Inside such code you can even detect which CheckBox called the event to act on individual controls:

If CType(sender, CheckBox).Text = "Cup of Java" ....

Upvotes: 5

Related Questions