VB:如何循环遍历标签并更改其文本?



我的表单中有80个标签,它们的名字都是"lbl"然后是1到80之间的数字。我想循环通过他们,并设置他们的文本属性在一个连续的模式,例如1,2,3…80 .

Dim nr As Integer
For nr = 1 To 80
lbl(nr).Text = nr
Next nr

我现在有这个,但是它不工作。有人知道类似的代码,将做什么我需要?

修改如下:

lbl(nr).Text = nr

:

Controls("lbl" & nr).Text = nr

这里假设Labels直接在表单上。如果它们在Panel或其他容器上,则使用它的Controls集合而不是表单的集合。

'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

将代码放在自己的方法中可以保持整洁。然后,您可能有多个容器(如Panels),您希望在其中设置标签的文本,因此您可以将该容器作为参数传递给方法。

如果您想在某个前缀后使用标签名称中的数字,则可以检查该前缀,然后将名称设置为前缀后的所有内容,如下所示:

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

如果容器命名为"Panel1"你可以使用

SetLabelsText(Panel1)

或者如果标签直接在表单上而不在表单的容器中:

SetLabelsText(Me)

使用这样的方法,您还可以将前缀作为参数传递,以使其更通用:

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

并将其命名为

SetLabelsText(Me, "lbl")

但是,如果标签以某种可以用一两个for循环创建的模式布局,那么以编程方式创建它们并同时设置文本可能会更容易一些。

听起来好像您有编号为1-80的控件,并且您想在循环中访问它们。

在运行循环之前,需要将它们放入数组中。如果您有任何嵌套的控件,这将需要查找表单上的所有文本框。

我写了一个小的测试应用程序,下面是我想到的:

首先是一个列表,用来存放我们想要处理的所有文本框,然后是一个子列表来填充它。

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)是一个返回控件中包含的所有textbox的函数。如果您只查看表单上的所有控件而不查看子控件,您将错过嵌套项。

我将在下面填充。

下一个函数从名称的末尾去掉数字,还有一个函数实际改变文本框:

' 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

最后,函数返回表单上的所有文本框作为一个列表。如果你知道你的表单只会包含最顶层的文本框,你可以通过MyForm.Controls循环并找到这些文本框,但这将适用于你的表单布局的任何方式。

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
现在,在Form的初始化器中,可以调用以下函数:
Public Sub New()
' This call is required by the designer.
InitializeComponent()
InitializeTextBoxes()
FillTextBoxes()
End Sub

这里有很多事情可以做得不同,但希望这是一个不错的开始,你需要做这样的事情。

当然,你可以为你想要查看的文本框的名称添加一个变量,或者改变你设置文本的方式;此外,如果您最终使控件乱序,您可以对列表进行排序,等等。

最新更新