Learner_51
Learner_51

Reputation: 1075

How to dynamically resize a textbox in C#

I am new to C# user interface. I have created a window as shown in the first image. But if the user drag the window and make it bigger, I would like for it to make each richTextBox expand with it as shown in the second image. For example, make the richTextBox4 bigger proportional to the size of the window. Any ideas would be greatly appreciated

Image1:

Image 1

Image 2: enter image description here

Upvotes: 4

Views: 18387

Answers (4)

danish
danish

Reputation: 5610

Make use of TableLayoutControl. You can have one control per cell of that table. Now, on that table, you set:

Dock: Fill
Column Width: Percentage or absolute value as per needs
Row Height: Percentage or absolute value as per needs

On the controls, like textboxes in your case, set Dock to Fill.

Read about it and try. If you get stuck, do come back with questions.

Upvotes: 5

Tomtom
Tomtom

Reputation: 9394

You can add a TableLayoutPanel to your Form. Per default this panel has two columns and two rows.

Then you add one TextBox to each cell and set the Dock-Property of each TextBox to Fill.

The last step thing to do is either set the Dock-Property of your TableLayoutPanel to Fill or set the Anchor-Property of your TableLayoutPanel to Left | Right | Bottom | Top. Then your panel will be resized together with the Form.

All these steps can be done with the designer.

Upvotes: 9

ajay gandhi
ajay gandhi

Reputation: 563

The same result can be achieved by using TableLayoutControl with one text box in each cell and then setting anchor property of each control (text boxes and TableLayout) to Left,Top, Right and bottom.

Upvotes: 1

Mihai Hantea
Mihai Hantea

Reputation: 1743

You can do it in designer.

Use the TableLayoutPanel, and set its Anchor property to Top,Bottom,Left,Right and for each RichtTextBox inside the cells set Dock to Fill.

this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
        this.button1 = new System.Windows.Forms.Button();
        this.richTextBox1 = new System.Windows.Forms.RichTextBox();
        this.richTextBox2 = new System.Windows.Forms.RichTextBox();
        this.richTextBox3 = new System.Windows.Forms.RichTextBox();
        this.richTextBox4 = new System.Windows.Forms.RichTextBox();
        this.tableLayoutPanel1.SuspendLayout();
        this.SuspendLayout();
        // 
        // tableLayoutPanel1
        // 
        this.tableLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right)));
        this.tableLayoutPanel1.ColumnCount = 2;
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); //this line sets the Column percentage
        this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
        this.tableLayoutPanel1.Controls.Add(this.richTextBox4, 1, 1);
        this.tableLayoutPanel1.Controls.Add(this.richTextBox3, 0, 1);
        this.tableLayoutPanel1.Controls.Add(this.richTextBox2, 1, 0);
        this.tableLayoutPanel1.Controls.Add(this.richTextBox1, 0, 0);
        this.tableLayoutPanel1.Location = new System.Drawing.Point(24, 80);
        this.tableLayoutPanel1.Name = "tableLayoutPanel1";
        this.tableLayoutPanel1.RowCount = 2;
        this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
        this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
        this.tableLayoutPanel1.Size = new System.Drawing.Size(861, 567);
        this.tableLayoutPanel1.TabIndex = 0;
        // 
        // button1
        // 
        this.button1.Location = new System.Drawing.Point(24, 27);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(137, 47);
        this.button1.TabIndex = 0;
        this.button1.Text = "button1";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click);
        // 
        // richTextBox1
        // 
        this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
        this.richTextBox1.Location = new System.Drawing.Point(3, 3);
        this.richTextBox1.Name = "richTextBox1";
        this.richTextBox1.Size = new System.Drawing.Size(424, 277);
        this.richTextBox1.TabIndex = 0;
        this.richTextBox1.Text = "";
        // 
        // richTextBox2
        // 
        this.richTextBox2.Dock = System.Windows.Forms.DockStyle.Fill;
        this.richTextBox2.Location = new System.Drawing.Point(433, 3);
        this.richTextBox2.Name = "richTextBox2";
        this.richTextBox2.Size = new System.Drawing.Size(425, 277);
        this.richTextBox2.TabIndex = 1;
        this.richTextBox2.Text = "";
        // 
        // richTextBox3
        // 
        this.richTextBox3.Dock = System.Windows.Forms.DockStyle.Fill;
        this.richTextBox3.Location = new System.Drawing.Point(3, 286);
        this.richTextBox3.Name = "richTextBox3";
        this.richTextBox3.Size = new System.Drawing.Size(424, 278);
        this.richTextBox3.TabIndex = 2;
        this.richTextBox3.Text = "";
        // 
        // richTextBox4
        // 
        this.richTextBox4.Dock = System.Windows.Forms.DockStyle.Fill;
        this.richTextBox4.Location = new System.Drawing.Point(433, 286);
        this.richTextBox4.Name = "richTextBox4";
        this.richTextBox4.Size = new System.Drawing.Size(425, 278);
        this.richTextBox4.TabIndex = 3;
        this.richTextBox4.Text = "";
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(922, 659);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.tableLayoutPanel1);
        this.Name = "Form1";
        this.Text = "Form1";
        this.tableLayoutPanel1.ResumeLayout(false);
        this.ResumeLayout(false);

Upvotes: 0

Related Questions