Dock order reversed on dynamically created controls

  • Thread starter Thread starter John B
  • Start date Start date
J

John B

Hello

I want to have a number of controls all docked left next to each other. When
I try the code below, the items are reversed with the last button nearest
the left instead of the first.

How can I fix this leaving the for loop the way it is?

Here is my code:

for (int i = 0; i < 10; i++)
{
Button btn = new Button();
btn.Text = "whatever" + i.ToString();
btn.Dock = DockStyle.Left;
this.Controls.Add(btn);
}

Thanks
 
John,

If I understand your question change the DockStyle.Left to DockStyle.Right.
If this is not what you mean please repost.

Hope this helps.
 
John,

After a closer look at your question I think that the code below is what you
are looking for. If not please repost.

Hope this helps!
--------------------

ArrayList al = new ArrayList();
for (int i = 0; i < 10; i++)
{
Button btn = new Button();
btn.Text = "whatever" + i.ToString();
btn.Dock = DockStyle.Left;
al.Add(btn);
}
al.Reverse();
foreach(Button b in al)
this.Controls.Add(b);
 
Brian Brown said:
John,

If I understand your question change the DockStyle.Left to DockStyle.Right.
If this is not what you mean please repost.

Hope this helps.

No, I actually want all the items to be docked left. It is the order which
concerns me.

I assumed that as the loop creates the button (labelled whaever1) first,
this should be left-most and subsequent buttons would be to the right of the
first one.

Here is the code again.

for (int i = 0; i < 10; i++)
{
Button btn = new Button();
btn.Text = "whatever" + i.ToString();
btn.Dock = DockStyle.Left;
this.Controls.Add(btn);
}

This produces the order (left to right) whatever9 ....whatever1. I want it
the other way around starting with whatever1 through to whatever9.

Thanks
 
So the last control added is MOST left. This is strange and is not what you
get when you manually add controls to the form in the designer. Here the
first controls stays as the left-most one. Can you confirm this.
 
John,

This is the same behavior that you get when you add controls via the
designer. You can test this by manually adding a button and then setting its
DockStyle property to Left. Now switch this to code view and look at the
section in InitializeComponent(). You should see:

this.button1 = new System.Windows.Forms.Button();
....
this.Controls.Add(this.button1);

Now repeat this and switch back to code view. Now you will see:

this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
....

this.Controls.Add(this.button2);
this.Controls.Add(this.button1);

As you can see the Controls.Add for button2 is above the Controls.Add for
button1. This behavior is the same as the code that I posted earlier
reversing the array of buttons and then appending them to the Control
Collection. So the designer does "in a way" reverse the adding of the buttons
to the control collection.

I hope this helps.
 
Back
Top