Question

I am using simple Panel and added two labels on event of button click.

Code looks like.(ShowAllItemContainer is Panel name.)

Label l = new Label();
l.Text = "Hello1";

Label ll = new Label();
ll.Text = "Hello2";

ll.Margin = new Padding(50,50, 0, 0);
ShowAllItemContainer.Controls.Add(l);
ShowAllItemContainer.Controls.Add(ll);

But output displaying only one lable, the first one.

I tried by setting padding but output remains same.

I want to add Labels vertically in Panel. How can I do that?

Was it helpful?

Solution

You have to specify the location

 Label l = new Label();
 l.Text = "Hello1";
 l.Location = new Point(0, 0);    
 Label ll = new Label();
 ll.Text = "Hello2";
 l.Location = new Point(0, 20);    
 ll.Margin = new Padding(50, 50, 0, 0);    
 ShowAllItemContainer.Controls.Add(l);
 ShowAllItemContainer.Controls.Add(ll);

Also you can achieve it using flowlayout panel

Label l = new Label();
l.Text = "Hello1";          
Label ll = new Label();
ll.Text = "Hello2";         
flowLayoutPanel1.Controls.Add(l);
flowLayoutPanel1.Controls.Add(ll);
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top