以编程方式向面板添加标签

时间:2016-05-26 01:14:01

标签: .net vb.net label

我正在尝试以编程方式向面板添加标签。当我添加具有相同Y轴的标签并错开X时,只有第一个标签显示。当我盯着X和Y时,他们都表现出来。我将Panel1.Controls.Add(标签(i))移动到另一个未显示的区域。下面我评论了在同一Y轴上显示的所有标签的intY_location。

        Dim labels(100) As Label


    For i = 0 To 100

        labels(i) = New Label
        Panel1.Controls.Add(labels(i))
        '   labels(i).Font = 1
        '   Label1.Font = New Font(labels(i).Font.Name, labels(i).Font.Size + 10, labels(i).Font.Style)

    Next i



            For i = 0 To 10
            '     Panel1.Controls.Add(labels(i))
            intX_Location = (intX * i) '+ (intSpacer * i) + (intPixel_Width / 2) '//Offset
            intY_Location = 50 '(i * intY) + (intSpacer * i) + (intPixel_Width / 2)
            labels(i).Text = "i"
            labels(i).Visible = True
            labels(i).Location = New Point(intX_Location, intY_Location)
        Next i

我做了一些更改,将标签从右到左放置,并根据需要显示。谁知道为什么?这是代码

            '// Start with Row to show column numbers
        For i = intColumnCount To 0 Step -1

            intX_Location = (50 * i) + intPixel_Width '+ (intSpacer * i) + (intPixel_Width / 2) '//Offset
            intY_Location = 10 '(i * intY) + (intSpacer * i) + (intPixel_Width / 2)

            Panel1.Controls.Add(labels(i))
            labels(i).Location = New Point(intX_Location, intY_Location)

        Next i

由于标签将根据用户具有新位置,因此我还添加了代码以在每次更新时从面板中删除标签。这将刷新标签位置和属性。我还添加了标签宽度,解决了我写过的问题。

        For i = 0 To 100
        Panel1.Controls.Remove(labels(i))
    Next i

    For i = 0 To 100
        labels(i) = New Label
        labels(i).Width = 10
        labels(i).Text = i.ToString() + 1
        labels(i).Name = "labelControl" + i.ToString()
        '    Panel1.Controls.Add(labels(i))
    Next i

1 个答案:

答案 0 :(得分:3)

这是一个完整的工作示例。

我不明白你的代码。在第一个循环中,您为10个标签设置了文本,但我们看不到您创建它们的位置,并且您注释了Panel1.controls.add。

在第二个循环中,您创建100个标签并将其添加到面板但不设置任何文本(因此,它们将创建为空且不可见)。

您需要创建标签,设置一些文字,给它一个位置和大小,如下例所示。

Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
           Dim PanelsText as New list(of string) from 
    {
        "Label 1",
        "Label 2",
        "I am the best Label ever !",
        "Am I a Label ?",
        "Dumb Label",
        "Definitely not a Label",
        "An ordinary label",
        "Bacon Topped label"
    }


    Dim Position as integer = 1
    For each LabelText as string in PanelsText
        Dim Label as New Label() with 
            {
            .Text = labeltext,
            .Location = New point(10,25 * Position),
            .Height = 23,
            .Width = 200
           }


        Panel1.controls.add(Label)
        Position+=1
    Next




End Sub

Ultimate label tester}

- 水平标签 -

根据你的评论,我现在明白你在水平标签放置方面遇到了麻烦。为了使其起作用,您需要在计算中包含标签宽度以放置标签。否则,标签不透明。使用100像素宽度的标签会隐藏100 px下的其他标签,即使标签仅使用其中10个像素。

以此代码为例。

private sub PopulateLabels()
      Static InitialPaddingX As Integer = 10
    Static InitialPaddingY As Integer = 15
    Static LabelWidth As Integer = 150
    Static LabelHeight As Integer = 23
    Static LabelPaddingX = 5
    Static LabelPaddingY = 5

    Dim PanelsText As New List(Of String) From
{
 "Label 1",
 "Label 2",
 "I am the best Label ever !",
 "Am I a Label ?",
 "Dumb Label",
 "Definitely not a Label",
 "An ordinary label",
 "Bacon Topped label"
}

    Dim Column As Integer = 0
    Dim Row As Integer = 0
    For Each LabelText As String In PanelsText
        Dim Label As New Label()
        With Label
            .Text = LabelText
            .Size = New Size(LabelWidth, LabelHeight)
            .Left = InitialPaddingX + (Column * LabelWidth + LabelPaddingX)
            .Top = InitialPaddingY + (Row & LabelHeight + LabelPaddingY)
        End With

        Panel1.Controls.Add(Label)

        If Column = 2 Then
            Column = -1
            Row += 1
        End If
        Column += 1

    Next
End sub

Ultimate Label tester - Now with horizontal feature