Question A better way to clear labels?

ppierpont

New member
Joined
Sep 25, 2012
Messages
2
Programming Experience
Beginner
I'm fairly new to programming, and have a question about clearing labels. After the program has run once, I want to clear the text out of the labels as soon as the user touches anything on the form. The below code works just fine, but I know there must be a shorter way to say it. Thanks for any advise you have, appreciate it!!
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub


    Private Sub radCoffee_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radCoffee.CheckedChanged
        ClearLabels()
    End Sub


    Private Sub radNoCoffee_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radNoCoffee.CheckedChanged
        ClearLabels()
    End Sub


    Private Sub radFilled_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radFilled.CheckedChanged
        ClearLabels()
    End Sub


    Private Sub radChocolate_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radChocolate.CheckedChanged
        ClearLabels()
    End Sub


    Private Sub radNoDonut_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radNoDonut.CheckedChanged
        ClearLabels()
    End Sub


    Private Sub radSugar_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radSugar.CheckedChanged
        ClearLabels()
    End Sub


    Private Sub radGlazed_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radGlazed.CheckedChanged
        ClearLabels()
    End Sub
End Class
 
Last edited by a moderator:
Your questions isn't actually about clearing Labels. It's about how to handle events. You can handle more than one event with the same method simply by adding multiple events to the Handles clause, e.g.
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub RadioButtons_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles radCoffee.CheckedChanged, radNoCoffee.CheckedChanged, radFilled.CheckedChanged, radChocolate.CheckedChanged, radNoDonut.CheckedChanged, radSugar.CheckedChanged, radGlazed.CheckedChanged
        ClearLabels()
    End Sub

End Class
You can have the IDE generate an event handler like that for you if you want. You can select multiple items in the designer, open the Properties window, click the Events button and then double-click the appropriate event. You can also select an existing handler for an event from its drop-down list.
 
thank u

That is exactly what I was looking for!! I did not realize "Radio_Buttons" exists, or that using it would refer to all the radio buttons on the form. Thanks for sharing this, I really do appreciate it!
 
That is exactly what I was looking for!! I did not realize "Radio_Buttons" exists, or that using it would refer to all the radio buttons on the form. Thanks for sharing this, I really do appreciate it!

"Radio_Buttons" doesn't exist. I just simply edited your code and changed the method name, because a name that refers to one specific RadioButton isn't appropriate for a method that handles events for all of them. The name of the method doesn't actually matter. It could be anything you like and it would still work. It makes sense to use something descriptive so that it's obvious what it does when you look at it but it's only the Handles clause that matters. It specifies what events the method handles.
 
Back
Top