Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have simple windows form for obtaining the user details. If the user wish to add alternate mobile number by clicking "+" Button , a text box should appear below it and other text boxes should re position themselves according to newly added text box. I am able to add the text box dynamically at run time but not able to re position the other form component with respect to dynamically added text box. Below is my code and form snapshot. Thanks in advance.

What I have tried:

I am able to add the text box dynamically at run time but not able to re position the other form component with respect to dynamically added text box. Below is my code and form snapshot. Thanks in advance.
C#
TextBox txtRun = new TextBox();
txtRun.Name = "txtDynamic" + c++;
txtRun.Location = new System.Drawing.Point(90, 74 + (20 * c));
txtRun.Size = new System.Drawing.Size(200, 25);
txtRun.Location.X = 90;
txtRun.Location.Y = 74;
this.Controls.Add(txtRun);
Posted
Updated 1-Mar-17 10:12am
v3
Comments
Ralf Meier 17-Feb-17 3:18am    
Why not ... where do you stuck ?
Where is the new TextBox displayed and which other controls should move to where ?

So better stop using fixed positions and sizes for your controls - we have 2017 now - everything dynamic and Content-sized ;)
No, it's not only joking - move away from Location/Size Control positioning, use (I suspect WinFoms - why not Tag your question accordingly?) Anchor and Dock concepts - also learn about Z-Order...
I strongly recommend to create a simple sample Form and play arround wiht Dock and Anchor to get a feel for it (and the Z-order) - I learned this "thinking" a long time ago with native C++ Windows API - when .NET arrived I never had to use the designer to position my controls - could do it in my head ;)
Btw. we have WPF now - where fixed Control positioninig/sizing is a rare exception...

So I hope you got what I wanted to say: Don't write an algorithm to calculate new positions and sizes, use the build in Anchor/Dock logic.

Kind regards
Johannes
 
Share this answer
 
Comments
CHill60 2-Mar-17 4:59am    
5'd to counter the 1-vote. This is sage advice.
johannesnestler 2-Mar-17 5:12am    
thank you CHill - maybe I shouldn't be so sarcastic :|
int cleft = 1;

TextBox txt = new TextBox();
this.Controls.Add(txt);
txt.Top = cleft * 40;
txt.Size = new Size(200, 16);
txt.Left = 150;
cleft = cleft + 1;
return;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900