Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi Team,
Once again I have one more problem in win-forms.
How to resize my win-form automatically when new controls are added dynamically?
I think it will be best for me to explain it theoretically:
Suppose I have 3 textboxes one below one and every textbox is accompanied with a Button as ADD MORE beside it.
3 textboxes are from designer and suppose when one clicks ADD MORE button of particular textbox 2 Textboxes should be populated below that textbox and the form should be resized so that remaining textboxes' layout doesnt get affected.

Please don't down vote me for this because I know how to add controls dynamically but i dont have any idea about resizing the whole form accordingly.

Sample examples would be great for me.


See this is what i have done till now:

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.AutoSize = true;
            this.Padding = new Padding(0, 0, 20, 20);
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TextBox tx1_1 = new TextBox();
            TextBox tx1_2 = new TextBox();
            tx1_1.Location = new Point(textBox1.Location.X,textBox1.Location.Y+25);
            tx1_2.Location = new Point(textBox1.Location.X, textBox1.Location.Y + 50);
            this.Controls.Add(tx1_1);
            this.Controls.Add(tx1_2);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            TextBox tx2_1 = new TextBox();
            TextBox tx2_2 = new TextBox();
            tx2_1.Location = new Point(textBox2.Location.X, textBox2.Location.Y + 25);
            tx2_2.Location = new Point(textBox2.Location.X, textBox2.Location.Y + 50);
            this.Controls.Add(tx2_1);
            this.Controls.Add(tx2_2);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            TextBox tx3_1 = new TextBox();
            TextBox tx3_2 = new TextBox();
            tx3_1.Location = new Point(textBox3.Location.X, textBox3.Location.Y + 25);
            tx3_2.Location = new Point(textBox3.Location.X, textBox3.Location.Y + 50);
            this.Controls.Add(tx3_1);
            this.Controls.Add(tx3_2);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            TextBox tx4_1 = new TextBox();
            TextBox tx4_2 = new TextBox();
            tx4_1.Location = new Point(textBox4.Location.X, textBox4.Location.Y + 25);
            tx4_2.Location = new Point(textBox4.Location.X, textBox4.Location.Y + 50);
            this.Controls.Add(tx4_1);
            this.Controls.Add(tx4_2);
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}


textboxes are getting overlapped on each other.
Posted
Updated 1-Jan-15 21:24pm
v4
Comments
BillWoodruff 2-Jan-15 3:00am    
"3 textboxes one below one" I read this as saying the TextBoxes are in vertical order.

"ADD MORE button of particular textbox 2 Textboxes should be populated below that textbox" I read this as saying add another TextBox in vertical order positioned underneath the TextBox whose corresponding Button was clicked.

All the TextBoxes including those added at run-time are in vertical order ?
[no name] 2-Jan-15 3:04am    
Yes Exactly...Thats what I mean,would you please help me?
[no name] 2-Jan-15 3:18am    
Please see the updated question I have added my source code.
BillWoodruff 2-Jan-15 3:39am    
Before I respond, I want to make sure I understand the display order of the first four TextBoxes: are they laid-out vertically or horizontally ?

If they are in vertical order then they may have to be moved every time you add new TextBoxes ... yes ?
[no name] 2-Jan-15 3:41am    
Yes they are laid out vertically and they have to be moved every time you add new TextBox.

First of all, adding a control as a very basic operation every System.Windows.Forms developer should know:
C#
Control parent = //... could be form or any other container control,
                 // say, Panel, TabPage, etc.
Control child = new CheckBox(); // or any other control;
                                // this is just an example or:
Control anotherChild = new TextBox();
//... set location, size, text, Dock and all other properties
//    of the child controls
child.Parent = parent; // one way
parent.Controls.Add(anotherChild); // another way, same effect as above
// ...

To change the form size, assign new value to the property Form.Size. The actual amount to add to Width and Height (you can use these properties separately, if you prefer) depends on your layout.

Good luck.
Happy New Year!

—SA
 
Share this answer
 
Comments
[no name] 2-Jan-15 3:15am    
See this is what i have done: problem is this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
this.AutoSize = true;
this.Padding = new Padding(0, 0, 20, 20);
this.StartPosition = FormStartPosition.CenterScreen;
}

private void button1_Click(object sender, EventArgs e)
{
TextBox tx1_1 = new TextBox();
TextBox tx1_2 = new TextBox();
tx1_1.Location = new Point(textBox1.Location.X,textBox1.Location.Y+25);
tx1_2.Location = new Point(textBox1.Location.X, textBox1.Location.Y + 50);
this.Controls.Add(tx1_1);
this.Controls.Add(tx1_2);

}

private void button2_Click(object sender, EventArgs e)
{
TextBox tx2_1 = new TextBox();
TextBox tx2_2 = new TextBox();
tx2_1.Location = new Point(textBox2.Location.X, textBox2.Location.Y + 25);
tx2_2.Location = new Point(textBox2.Location.X, textBox2.Location.Y + 50);
this.Controls.Add(tx2_1);
this.Controls.Add(tx2_2);
}

private void button3_Click(object sender, EventArgs e)
{
TextBox tx3_1 = new TextBox();
TextBox tx3_2 = new TextBox();
tx3_1.Location = new Point(textBox3.Location.X, textBox3.Location.Y + 25);
tx3_2.Location = new Point(textBox3.Location.X, textBox3.Location.Y + 50);
this.Controls.Add(tx3_1);
this.Controls.Add(tx3_2);
}

private void button4_Click(object sender, EventArgs e)
{
TextBox tx4_1 = new TextBox();
TextBox tx4_2 = new TextBox();
tx4_1.Location = new Point(textBox3.Location.X, textBox3.Location.Y + 25);
tx4_2.Location = new Point(textBox3.Location.X, textBox3.Location.Y + 50);
this.Controls.Add(tx4_1);
this.Controls.Add(tx4_2);
}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}
Sergey Alexandrovich Kryukov 2-Jan-15 3:38am    
Well, "problem is this" and then some code dump. It does not explain the problem. Note that you could see what I explained from this auto-generated code. Which part of the techniques is not clear?
—SA
BillWoodruff 2-Jan-15 6:07am    
+1 Note that the OP clearly told you: "I know how to add controls dynamically."
I hope you would get a scroll when you add buttons on the form.My suggestion would be

1.Set Initial form's height and width on form_load.

2.When click on the "AddMore_button",Increase the width and height size dynamically.

C#
private void Form1_Load(object sender, EventArgs e)
        {
            Width = 100;
            Height = 100;
        }

        private void AddMore_Click(object sender, EventArgs e)
        {
            width+=10;
            Height+=10;
        }
 
Share this answer
 
v2

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