VB.Net - Manage events and properties for multiple controls

December 2016




This demonstration aims to:
  • Group Click and TextBox_Changed evens of six TextBox but in one Sub
  • Include six label in a collection which allows for indexes.

Prerequisites


Open a new Windows (form) project.
Paste six textboxes, the name does not matter but you must enter the number (1 to 6) in the Tag property.
Paste six labels, the name does not matter but you must enter the number (1 to 6) in the Tag property.
Paste a label (bottom) with the name: Label14
The six labels to the left the TextBox are optional and are only used as a benchmark.

FormLoad Code


The demo is based on six stextbox and label but the number, but this can be easily changed:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load  
        Dim Ctr As Control  
        For Each Ctr In Me.Controls  
            If TypeOf Ctr Is TextBox Then  
                'Initialise les événements des TextBox  
                AddHandler Ctr.TextChanged, AddressOf AllTextBox_TextChanged  
                AddHandler Ctr.Click, AddressOf AllTextBox_Click  
            ElseIf TypeOf Ctr Is Label Then  
                If Ctr.Tag <> "" Then  
                    'Initialise les propriétés des labels  
                    LbGroupe.Add(Ctr, Ctr.Tag)  
                End If  
            End If  
        Next  
    End Sub



Downloading

==Click and Textboxes_Changed code=
Private Sub AllTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)  
        LbGroupe(sender.tag).text = sender.text  
    End Sub  
    Private Sub AllTextBox_Click(ByVal sender As Object, ByVal e As System.EventArgs)  
        Label14.Text = "Vous avez sélectionner le TexBox N° " & sender.tag  
    End Sub

Download


If you wish to download a demo, click here

Related :

This document entitled « VB.Net - Manage events and properties for multiple controls » from CCM (ccm.net) is made available under the Creative Commons license. You can copy, modify copies of this page, under the conditions stipulated by the license, as this note appears clearly.