Reputation: 71
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
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