Bobby Bobby - 6 months ago 54
Vb.net Question

programmatically add label to panel

I am trying to add a label programmatically to a panel. When I add the labels with same Y axis and stagger the X the only the first labels show. When I stager both the X and Y they all show. I moved the Panel1.Controls.Add(labels(i))to another area not shown. Below I commented the intY_location for all labels to show on the same Y axis.

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


I made some changes to place the labels from right to left and they all show as needed. Anyone know why ? Here is the code

'// 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


Since the labels will have new locations depending on the user I also added code to remove labels from panel each time there is an update. This will refresh labels location and properties. I also added the label width which solved the issue I was having with over write.

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

Answer

Here is a complete working example.

I do not understand your code. In the first of your loops, you set the text for 10 labels but we do not see where you create them and you commented the Panel1.controls.add.

In the second loop, you create 100 label and add them to your panel but do not set any text (therefore, they will be create empty and invisible).

You need to create your label, set some text, give it a location and size, such at the example below.

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 }

-- Horizontal labels --

Following your comment, I now understand you have trouble with horizontal label placement. In order for it to work, you need to include the label width in your calculation for placing the labels. Otherwise, labels are not transparent. A label using 100 pixel width will hide other label under that 100 px, even if the label make use of only 10 of these pixels.

Take for example this code.

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

Comments