简体   繁体   English

如何在C#中动态调整文本框的大小

[英]How to dynamically resize a textbox in C#

I am new to C# user interface. 我是C#用户界面的新手。 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. 但是,如果用户拖动窗口并使其扩大,我希望它使每个richTextBox随其展开,如第二richTextBox图所示。 For example, make the richTextBox4 bigger proportional to the size of the window. 例如,使richTextBox4与窗口大小成正比。 Any ideas would be greatly appreciated 任何想法将不胜感激

Image1: 图片1:

图片1

Image 2: 图片2: 在此处输入图片说明

You can add a TableLayoutPanel to your Form. 您可以将TableLayoutPanel添加到您的窗体。 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 . 然后,向每个单元格添加一个TextBox并将每个TextBoxDock Property设置为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 最后一步要做的要么设置Dock ,物业你的TableLayoutPanelFill或设置Anchor你的,物业TableLayoutPanel ,以Left | Right | Bottom | Top Left | Right | Bottom | Top Left | Right | Bottom | Top . Left | Right | Bottom | Top Then your panel will be resized together with the Form. 然后,您的面板将与表单一起调整大小。

All these steps can be done with the designer. 所有这些步骤都可以由设计人员完成。

Make use of TableLayoutControl . 利用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 . 在控件上(如您的情况下的文本框),将Dock设置为Fill

Read about it and try. 阅读并尝试。 If you get stuck, do come back with questions. 如果您遇到困难,请务必提出问题。

通过将TableLayoutControl与每个单元格中的一个文本框一起使用,然后将每个控件的锚点属性(文本框和TableLayout)设置为Left,Top,Right和bottom,可以实现相同的结果。

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 . 使用TableLayoutPanel并将其Anchor属性设置为TopBottomLeftRight并为单元格内的每个RichtTextBox设置DockFill

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM