thisguy123
thisguy123

Reputation: 945

Why don't my Labels show in VB.NET Form

After reading this question, I wrote some code to create a label for each attribute of an xml element.

The problem is that when I run the project, my form only displays the first label. I've checked in the immediate window as well as debug window and all of the labels are loaded to the form, but none of them are displayed. Help?

Here's the code that runs when the form loads.

Dim doc As New XmlDocument()
doc.Load("xmlfile")
Dim ability As XmlNode = doc.GetElementsByTagName("ability").Item(0)
Dim numberofLabels = ability.Attributes.Count
ReDim labels(numberofLabels)
For counter As Integer = 0 To numberofLabels - 1
    labels(counter) = New Label
    labels(counter).Visible = True
    labels(counter).Text = ability.Attributes.Item(counter).Name
    labels(counter).Location = New System.Drawing.Point(10, 30 + counter * 10)
    Me.Controls.Add(labels(counter))
Next

Upvotes: 3

Views: 2412

Answers (2)

Victor Zakharov
Victor Zakharov

Reputation: 26464

You should be using some layout manager, to help you with control positioning. Doing it manually is not worth the pain. Try using TableLayoutPanel or FlowLayoutPanel. Both can be docked or anchored to a parent control, so everything behaves very smoothly. Otherwise you are looking to write a lot of positioning/resizing code, and then maintaining it later.

Upvotes: 3

NoChance
NoChance

Reputation: 5772

Change the value of 10 in the original code line for a new point to a bigger value such as 40, so that new labels could appear separated visually:

labels(counter).Location = New System.Drawing.Point(10 + counter, 30 + counter * 40)

Upvotes: 0

Related Questions