Hi there,
I want to know how to insert a control at runtime in between existing controls on my form. In other words, how do I proceed to push down the controls that should be appearing after my newly inserted control?
Thanks!
Emmi
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Scott LyslePosted May 26, 2008, 9:57 PM
You will have to get the locations on the controls that are going to be moved, make up a location for the inserted control and then add the control after setting its location. If you have five textboxes on a form and you want to put a new one it between the forth and the fifth textboxes, you could do something like this:
// get the current location of
// textbox 5 (this is the one to displace)
Point loc5 = textBox5.Location;
// move the new location of textbox5
// down 26 - make a hole for the
// new textbox at the same spacing used
// on the other textboxes
Point newLoc = new Point(loc5.X, (loc5.Y+26));
textBox5.Location = newLoc;
// id them
textBox1.Text = "One";
textBox2.Text = "Two";
textBox3.Text = "Three";
textBox4.Text = "Four";
textBox5.Text = "Five";
// Insert new textbox at the old location
// of textbox5
System.Windows.Forms.TextBox tRex = new TextBox();
tRex.Text = "T-REX";
tRex.Location = loc5;
this.Controls.Add(tRex);