在vb.net中创建多个标签并添加文本

时间:2015-10-23 07:59:03

标签: vb.net label

我有一个KeyValuePair的列表,其中KeyValuePair的数量随时间而变化。在表单中,我需要为包含KeyValuePair作为文本的每个Value创建一个标签。

我该怎么做?

这是我试过的......

 For Each element In keyvalueP
            Dim Label1 As New Label
            With Label1
                .Width = 512
                .Height = 18
                .Left = 12
                .Text = " Nr. " & i & " - Matchtype " & element.Value.ToString
            End With
            i = i + 1
Next

1 个答案:

答案 0 :(得分:0)

我实际上设法找到一个有效的解决方案

Private Sub R75Dialog_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       Dim labelText = ""
       Dim i = 0
       For Each element In keyvalueP
       labelText = " Nr. " & i & " Groupname:  " & element.Key.ToString & " - Matchtype " & element.Value.ToString
       Dim label As New Label()
       Dim count As Integer = Panel1.Controls.OfType(Of Label)().ToList().Count
       label.Location = New Point(10, (25 * count) + 2)
       label.Size = New Size(1000, 20)
       label.Name = "label_" & (count + 1)
       label.Text = labelText
       Panel1.Controls.Add(label)
       i = i + 1
    Next
End Sub