Having problem with order of controls when inserting controls into a panel.

  • Thread starter Thread starter Ken Varn
  • Start date Start date
K

Ken Varn

I am confused about the orderring of controls when I progmatically insert
controls into a Panel.

I have two controls that are both set to dock left. I insert the first
control, and then the second, but when I display the control the order is
reversed. Can someone explain why this happens?

i.e.,

{
Panel MyPanel = new Panel();
Label Label1 = new Label();
Label Label2 = new Label();

Label1.Text = "Label1";
Label1.Dock = DockStyle.Left;
Label2.Text = "Label2";
Label2.Dock = DockStyle.Left;

MyPanel.Controls.Add(Label1);
MyPanel.Controls.Add(Label2);

// At this point I would expect the panel to look like this
[Label1Label2], but instead it is [Label2Label1]
}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
(e-mail address removed)
-----------------------------------
 
Hi Ken,
The controls are layouted starting from the last in the list. So, the first
control placed and sized will be the last one in the list. In your case this
is Label2. BTW, this is the reason why the *fill* docked control has to be
inserted first in the controls list.

HTH
B\rgds
100
 
I don't think so. But you can insert the Label2 before the Label1.
If you do that in a loop or you get the reference to Label1 first, you can
use:

Controls.Insert(0, label)
instead of
Controls.Add(label)

HTH
B\rgds
100

Ken Varn said:
I am guessing that there is no way to tell the panel to use first-in
ordering?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
(e-mail address removed)
-----------------------------------
100 said:
Hi Ken,
The controls are layouted starting from the last in the list. So, the first
control placed and sized will be the last one in the list. In your case this
is Label2. BTW, this is the reason why the *fill* docked control has to be
inserted first in the controls list.

HTH
B\rgds
100
Ken Varn said:
I am confused about the orderring of controls when I progmatically insert
controls into a Panel.

I have two controls that are both set to dock left. I insert the first
control, and then the second, but when I display the control the order is
reversed. Can someone explain why this happens?

i.e.,

{
Panel MyPanel = new Panel();
Label Label1 = new Label();
Label Label2 = new Label();

Label1.Text = "Label1";
Label1.Dock = DockStyle.Left;
Label2.Text = "Label2";
Label2.Dock = DockStyle.Left;

MyPanel.Controls.Add(Label1);
MyPanel.Controls.Add(Label2);

// At this point I would expect the panel to look like this
[Label1Label2], but instead it is [Label2Label1]
}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
(e-mail address removed)
 
I am guessing that there is no way to tell the panel to use first-in
ordering?

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
(e-mail address removed)
-----------------------------------
100 said:
Hi Ken,
The controls are layouted starting from the last in the list. So, the first
control placed and sized will be the last one in the list. In your case this
is Label2. BTW, this is the reason why the *fill* docked control has to be
inserted first in the controls list.

HTH
B\rgds
100
Ken Varn said:
I am confused about the orderring of controls when I progmatically insert
controls into a Panel.

I have two controls that are both set to dock left. I insert the first
control, and then the second, but when I display the control the order is
reversed. Can someone explain why this happens?

i.e.,

{
Panel MyPanel = new Panel();
Label Label1 = new Label();
Label Label2 = new Label();

Label1.Text = "Label1";
Label1.Dock = DockStyle.Left;
Label2.Text = "Label2";
Label2.Dock = DockStyle.Left;

MyPanel.Controls.Add(Label1);
MyPanel.Controls.Add(Label2);

// At this point I would expect the panel to look like this
[Label1Label2], but instead it is [Label2Label1]
}

--
-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.
(e-mail address removed)
 
Back
Top