Dinesh Suthar

Dinesh Suthar

  • NA
  • 4
  • 2.2k

how to resize winform while controls are added dynamically?

Jan 2 2015 3:37 AM
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.
 

This is what I have done till Now but the textboxes are getting overlapped on each other.

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)
        {
 
        }
    }
}

Answers (3)