I have 80 labels on my form, all named "lbl" and a number from 1 to 80 after that. I want to loop through them and set their Text property in a consecutive pattern e.g. 1, 2, 3 ... 80.
Dim nr As Integer
For nr = 1 To 80
lbl(nr).Text = nr
Next nr
I have this right now, but it doesn't work. Does anyone know of similar code that will do what I need?
CodePudding user response:
Change this:
lbl(nr).Text = nr
to this:
Controls("lbl" & nr).Text = nr
This assumes that the Labels
are directly on the form. If they are on a Panel
or some other container, use it's Controls
collection instead of that of the form.
CodePudding user response:
'in WPF
Dim i As Integer = 1
For Each c In Me.gridMain.Children
If c.GetType() Is GetType(Label) AndAlso c.Name Like "lbl*" Then
Dim _label As Label = c
_label.Content = "label" & i
i = 1
End If
Next
'in Winform
Dim i As Integer = 1
For Each c In Me.Controls
If c.GetType() Is GetType(Label) AndAlso c.Name Like "lbl*" Then
Dim _label As Label = c
_label.Text = "label" & i
i = 1
End If
Next
CodePudding user response:
It would keep things tidy to put the code to do that in its own method. Then, you might have more than one container (such as Panels) that you want to set the text of the labels in, so you could pass that container as a parameter to the method.
As you want to use the number from the name of the label after some prefix, you can check for that prefix and then set the name to everything after the prefix, like this:
Sub SetLabelsText(container As Control)
Const prefix = "lbl"
For Each ctrl In container.Controls.OfType(Of Label)
If ctrl.Name.StartsWith(prefix) Then
ctrl.Text = ctrl.Name.Substring(prefix.Length)
End If
Next
End Sub
Then if the container was named "Panel1" you would use
SetLabelsText(Panel1)
or if the labels were directly on the form and not in a container on the form:
SetLabelsText(Me)
With a method like that, you could also pass the prefix as a parameter to make it more versatile:
Sub SetLabelsText(container As Control, prefix As String)
For Each ctrl In container.Controls.OfType(Of Label)
If ctrl.Name.StartsWith(prefix) Then
ctrl.Text = ctrl.Name.Substring(prefix.Length)
End If
Next
End Sub
and call it like
SetLabelsText(Me, "lbl")
However, if the labels are laid out in some pattern that you could create with a for loop or two, it could be somewhat easier to create them programatically and set the text at the same time.
CodePudding user response:
It sounds like you have controls numbered from 1-80 and you want to access them in a loop.
You will need to actually put them in an array before you can run your loop. This will require finding all your textboxes on the form if you have any nested controls.
I wrote up a little test app and here's what I came up with:
First, a list to hold all the textboxes we want to process, and a sub to fill it.
Public numberTextBoxes As New List(Of TextBox)
' A function that finds all the desired textboxes and saves them
Public Sub InitializeTextBoxes()
Dim controls = GetAllTextBoxes(Me)
For Each formTextBox As TextBox In controls
Dim textBoxName As String = formTextBox.Name
If textBoxName.StartsWith("TextBox") Then
Dim textBoxNumber As Integer
' verify that this has a number after the TextBox part
If Integer.TryParse(textBoxName.Substring(7), textBoxNumber) Then
numberTextBoxes.Add(formTextBox)
End If
End If
Next formTextBox
End Sub
GetAllTextBoxes(Me)
is a function that returns all the TextBoxes contained on a control. If you just look through all the controls on the Form without looking through the child controls, you will miss nested items.
I'll fill that in below.
Next, a function to get the number off the end of the names, and a function to actually change the textboxes:
' extracts the number from the end of a textbox's string
Public Function GetTbNum(textBoxName As String) As Integer
If (textBoxName.Length > 2) Then
GetTbNum = CInt(textBoxName.Substring("TextBox".Length))
End If
End Function
' a function to change the names on the textboxes
Public Sub FillTextBoxes()
' Note that Arrays and Lists are usually 0-based
For i = 0 To numberTextBoxes.Count - 1
Dim tb As TextBox = numberTextBoxes(i)
' if you want to set the text to the number at the end of the name
numberTextBoxes(i).Text = GetTbNum(numberTextBoxes(i).Name)
Next i
End Sub
Finally, the function to return all the textboxes on a form as a list. If you know your Form only is ever going to contain the textboxes on the very top level, you could just loop through MyForm.Controls
and find those textboxes, but this will work on any way you have your form laid out.
Function GetAllTextBoxes(control As Control, Optional textBoxList As List(Of TextBox) = Nothing) As List(Of TextBox)
If textBoxList Is Nothing Then
textBoxList = New List(Of TextBox)
GetAllTextBoxes(control, textBoxList)
GetAllTextBoxes = textBoxList
Else
If (TypeOf control Is TextBox) Then
textBoxList.Add(control)
End If
For Each item In control.Controls
GetAllTextBoxes(item, textBoxList)
Next item
End If
End Function
Now, in the Form's intitializer, you can call the functions:
Public Sub New()
' This call is required by the designer.
InitializeComponent()
InitializeTextBoxes()
FillTextBoxes()
End Sub
There's lots of things that could be done differently here but hopefully that's a decent start on what you would need to do something like this.
Of course you could add a variable for the name of the textboxes you wanted to look at, or change how you set the text; also, you could sort the list if you end up getting your controls out-of-order, etc.