ElektroStudios
ElektroStudios

Reputation: 20464

How to use the same event for a number of controls?

I have a lot of checkboxes inside a panel and I want to ask if exist any way to reproduce a event action in all the checkboxes without need to write the same for the 20 checkboxes:

  Private Sub C1CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles C1CheckBox1.CheckedChanged
        C1CheckBox1.Text = "test"
    End Sub

EDIT:

I don't mean how to change the text property of all the checkboxes inside the C1CheckBox1 event, what I mean is how to clone that event for all the checkboxes, if one of all checkboxes is clicked then reproduce the same event for all, it's a little difference (I think)

UPDATE:

Pseudocode:

sub event that affect to all my checkboxes in the panel
    If ANY checkbox_CheckedChange then
       ALL_the_other_Checkboxes.Checked = False
    end if
end sub

The difference that I'm trying to talking about is I ask if this is possible only writing one event or something like, to not write the same property change in the 20 other checkbox events.

Upvotes: 0

Views: 39

Answers (1)

Nathan Koop
Nathan Koop

Reputation: 25197

You can handle the same event by doing a comma separated list of controls and their event.

EG:

Private Sub C1_CheckedChanged(sender As Object, e As EventArgs) 
    Handles C1CheckBox1.CheckedChanged, C1CheckBox2.CheckedChanged, ... C1CheckBox50.CheckedChanged
        ...
        Do Action
        ....
    End Sub

Upvotes: 2

Related Questions