473,387 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Dock order reversed on dynamically created controls

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
Nov 16 '05 #1
5 7774
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.
Nov 16 '05 #2
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);
Nov 16 '05 #3
"Brian Brown" <Br********@discussions.microsoft.com> wrote in message
news:5C**********************************@microsof t.com...
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
Nov 16 '05 #4
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.
"Brian Brown" <Br********@discussions.microsoft.com> wrote in message
news:79**********************************@microsof t.com...
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);

Nov 16 '05 #5
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.

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: AJP | last post by:
I have a child window which does the following after a button is pressed: Response.Write("<script>window.opener.__doPostBack('','');</script>"); but I get the following uncaught exception upon...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am have facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the...
1
by: Kamal Jeet Singh | last post by:
Hi Friends !! I am facing problem in controlling the dynamically created controls on web page. The problem Scenario is Scenario:- My requirement is to load the web user controls on the web...
5
by: Amelyan | last post by:
How can I get state of dynamically created controls (RadioButton, CheckBox, TextBox.Text) on post back when I click submit button? The only way I know is by traversing Response.Form enumberator;...
2
by: Nathan Sokalski | last post by:
I have several Button controls that I created dynamically that I need to handle the Click events for. How do I specify the event handler for dynamically created controls? Thanks. -- Nathan...
6
by: TB | last post by:
Hi All: I have this page where a rows / cells are programmatically added to to table by pushing a button. The rows contain a textbox and a associated button. What I want to is to be able to...
3
by: aroraamit81 | last post by:
How do I retain State for Dynamically Created Controls in ASP.NET. I have created a control on first time page load, but when I do post back, then my dynamic controls gets disappeared. I have used...
1
by: Grega | last post by:
Hi all, I am trying to create a VB smart device application. I create few buttons dynamically on form_load... something like this: Dim WithEvents BtnOperate As New...
2
by: Dica | last post by:
i've got a script that creates a new tablerow + tablecell and appends that to a non dynamically created table control. a new row is added for each recordset returned from my sql statement. within...
2
by: Dwight Johnson | last post by:
I am building a website in VS2005. I basically have one page, default.aspx, which contains placeholder objects into which I add various controls that are dynamically created. I have a dropdownlist...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.