VB.net - Replacing the index of the controls on a form

December 2016




Intro


For those who are already introduced to VB6 it is easy to create controls with the same name but with a different index.

Under VB.Net BG, things toughen; the collections of controls no longer exist.
  • It is therefore necessary to develop tricks to overcome this lack, but do not worry, everything is possible with VB.Net.
  • We can consider that all controls on a form are already part of a collection. From there we can try to identify them!
  • We can review all "control" making up the design of the form, but the main the difficulty is that each control possess its own properties.
  • This first approach we will treat the properties of multiple controls of different types with some lines of code in a single sub.

Preliminary


Open a new Windows Form project
  • Paste 4 textBox and set their Tag properties 1, 2, 3 and 4
  • Paste 6 picture box and set their Tag properties 1, 2, 3, 4...6
  • Paste 2 Buttons



Code

The code is split into two sub for a better readability, but it could be achieved by a single sub.
If you want you can cleant both lines: Img.image = ...
The images are in the zip.
'================================================= =============
'On the form,
'4 TextBox propriété Tag de 1 à 4
'6 PictureBox propriété Tag de 1 à 6
'==============================================================
Public Class Form1   
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load   
        InitPic()   
        InitText()   
    End Sub   
    Sub InitText()   
        Dim Ctl As Control   
        Dim Txt As TextBox   
        Static NbAlea As Integer   
        NbAlea += 1 : If NbAlea > 4 Then NbAlea = 1   
        For Each Ctl In Me.Controls 'boucle sur tout les contrôles de la forme   
            If TypeOf Ctl Is TextBox Then 'Vérifie que c'est un textbox   
                Txt = Ctl   
                If Txt.Tag = NbAlea Then   
                    'S'il n'y a que des TextBox sur la form il n'est pas nécessaire   
                    'de passer par une variable buffer   
                    Txt.Text = "c'est le TextBox : " & Txt.Name   
                    Txt.BackColor = Color.Fuchsia   
                Else   
                    Txt.Text = ""   
                    Txt.BackColor = Color.Empty   
                End If   
            End If   
        Next   
    End Sub   
    Sub InitPic()   
        Dim Pic As Control   
        Dim Img As PictureBox   
        Static NbAlea As Integer   
        NbAlea += 1 : If NbAlea > 6 Then NbAlea = 1   
        'S'il n'y a que des pictureBox sur la form il n'est pas nécessaire   
        'de passer par une variable buffer   
        For Each Pic In Me.Controls 'boucle sur tout les contrôles de la forme   
            If TypeOf Pic Is PictureBox Then   
                Img = Pic   
                If Pic.Tag = NbAlea Then   
                    Img.BackColor = Color.Aquamarine   
                    Img.BorderStyle = BorderStyle.Fixed3D   
                    'Img.Image = System.Drawing.Bitmap.FromFile(My.Application.Info.DirectoryPath & "\bt" & NbAlea & ".bmp")   
                    'Img.SizeMode = PictureBoxSizeMode.StretchImage   
                Else   
                    Img.BackColor = Color.Chocolate   
                    Img.BorderStyle = BorderStyle.FixedSingle   
                End If   
            End If   
        Next   
    End Sub   

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click   
        InitText()   
    End Sub   

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click   
        InitPic()   
    End Sub   
End Class


Download:


Related :

This document entitled « VB.net - Replacing the index of the controls on a form » 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.