如何在循环中更改具有类似设计名称的标签的文本属性?

时间:2018-02-20 18:11:55

标签: .net vb.net

我的表单上的表格设计布局中有几个标签。我想一次更改标签组的文本属性,例如,包含TempID的所有标签。它们要更改的值存储在数组中,因此lblTempID.text = CustomerID(counter)。我遇到的问题是,即使我得到表中包含TempID的所有标签的名称并将它们存储在数组中,我也不能使用.text方法,因为它是不是字符串的属性。我尝试过的其他解决方案只是在表单加载时导致所有标签都是白色矩形......

任何帮助表示赞赏!

4 个答案:

答案 0 :(得分:1)

使用Linq按名称获取标签

' say all the labels are directly on the form
' alternatively this could be a Panel, GroupBox, etc.
Dim container As Control = Me
' get all Labels in the container
Dim myLabels As IEnumerable(Of Label) = container.Controls.OfType(Of Label)
' get all labels with "TempID" in their name
Dim myLabelTempIDs As IEnumerable(Of Label) = myLabels.Where(Function(l) l.Name.Contains("TempID"))
' get the only label named exactly "lblTempID1"
Dim myLabelTempID1 As Label = myLabels.Where(Function(l) l.Name = "lblTempID1").Single()

For counter As Integer = 1 To 10
    Dim counterLocal = counter
    Dim myLabelTempID = myLabels.Where(Function(l) l.Name = $"lblTempID{counterLocal}").Single()
    myLabelTempID.Text = CustomerID(counter)
Next

使用扩展方法

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' get all Labels in Me, using extension method defined below
        Dim myLabels As IEnumerable(Of Label) = Me.ChildControls(Of Label)

        For counter As Integer = 1 To 10
            Dim counterLocal = counter
            Dim myLabelTempID = myLabels.Where(Function(l) l.Name = $"lblTempID{counterLocal}").Single()
            myLabelTempID.Text = CustomerID(counter)
        Next
    End Sub

End Class

Public Module extensions

    <Runtime.CompilerServices.Extension()>
    Public Function ChildControls(Of T As Control)(ByVal parent As Control) As IEnumerable(Of T)
        Dim result As New List(Of T)
        For Each ctrl As Control In parent.Controls
            If TypeOf ctrl Is T Then result.Add(CType(ctrl, T))
            result.AddRange(ctrl.ChildControls(Of T)())
        Next
        Return result
    End Function

End Module

答案 1 :(得分:1)

老实说,我不明白为什么其他人这么复杂(没有冒犯,但这是真的)。​​

使用For loopMe.Controls.Find()Me作为当前表单),您可以轻松访问顺序命名的标签。

'Assuming you have 50 labels:
For i = 1 To 50
    Dim FoundControls As Control() = Me.Controls.Find("lblTempID" & i, True) 'Find the label with the name "lblTempID#" - where # is the current index (1-50).
    If FoundControls.Count > 0 Then 'Was a label found?
        FoundControls(0).Text = CustomerID(i) 'Change its text.
    End If
Next

这将以递归方式查找标签(意味着它甚至可以在子容器(例如面板和组框)中查找它们)并更改它找到的文本。

唯一的背面是,如果您有多个标签具有完全相同的名称,但在不同的容器中,那么这只会更改它找到的第一个标签的文本。因此,只要您的标签具有唯一名称,这应该可以正常工作。

答案 2 :(得分:0)

  Dim LabelControls = New List(Of Label)
    LabelControls.Add(lblTempID1)
    LabelControls.Add(lblTempID2)
    LabelControls.Add(lblTempID3)
    LabelControls.Add(lblTempID4)
    LabelControls.Add(lblTempID5)
    LabelControls.Add(lblTempID6)
    LabelControls.Add(lblTempID7)
    LabelControls.Add(lblTempID8)
    LabelControls.Add(lblTempID9)
    LabelControls.Add(lblTempID10)  

 For Each label In LabelControls
        If label.Name.Contains("lblTempID") Then
            For counter = 0 To 9
                LabelControls(counter).Text = CStr(TempCustomerID(counter))
            Next

它与之前的答案非常相似(如果不完全相同),但它完成了我现在需要做的事情。

答案 3 :(得分:-2)

您需要将控件放在容器中。

var labControls = new List<Label>();
labControls.Add(TempID1);
labControls.Add(TempID2);
labControls.Add(TempID3);
....
foreach(var lab in labControls)
{
     lab.Text = "new text";
}