Laziale
Laziale

Reputation: 8225

Placement of labels in a panel winforms

I have a panel in winforms app in which I want to display some data in vertical alignment. I will get that data from XML document. I'll loop through XML like this:

for (int i = 0; i < node.ChildNodes.Count; i++)
{
    lbl = new Label();
    lbl.Text = node.ChildNodes[i].Name + " = " + node.ChildNodes[i].InnerText;
    panel1.Controls.Add(lbl);
}

At the end I can see only the first record displayed at the top left corner of the panel, but looping through the panel1.controls, I can see the count is 79, I just need to position them correctly.

How can I achieve that?

Upvotes: 2

Views: 4814

Answers (3)

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98848

  • Use FlowLayoutPanel instead of it.

  • Set to FlowDirection property to TopDown.

Upvotes: 1

malkassem
malkassem

Reputation: 1957

you just need to set the top and/or left properties of the label objects.

for (int i = 0; i < node.ChildNodes.Count; i++)
            {

                lbl = new Label();
                lbl.Text = node.ChildNodes[i].Name + " = " + node.ChildNodes[i].InnerText;
                lbl.top = 15 * i;
                panel1.Controls.Add(lbl);
}

Upvotes: -1

keyboardP
keyboardP

Reputation: 69372

You could use a FlowLayoutPanel instead and set its FlowDirection property to TopDown.

Upvotes: 2

Related Questions