如何在动态添加新控件时自动调整winform的大小? [英] how to resize a winform automatically when new controls are added dyanamically?
问题描述
Hi Team,
我再次遇到win-forms的问题。
如果动态添加新控件,如何自动调整win-form的大小? br />
我认为最好在理论上对它进行解释:
假设我有一个低于一的文本框,每个文本框旁边都有一个按钮,旁边还有一个按钮。
3个文本框来自设计师,并假设当单击特定文本框2的ADD 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:
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.
推荐答案
首先,将控件添加为每个基本操作System.Windows.Forms
开发人员应该知道:
First of all, adding a control as a very basic operation everySystem.Windows.Forms
developer should know:
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
// ...
要更改表单大小,请为其分配新值property Form.Size
。要添加到宽度
和高度
的实际金额(如果您愿意,可以单独使用这些属性)取决于您的布局。
祝你好运。
新年快乐!
< dd> -SA
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!
我希望你在表格上添加按钮时会得到一个滚动条。我的建议是
1.在 form_load 上设置初始表格的高度和宽度。
2.点击AddMore_button ,动态增加宽度和高度尺寸。
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.
private void Form1_Load(object sender, EventArgs e)
{
Width = 100;
Height = 100;
}
private void AddMore_Click(object sender, EventArgs e)
{
width+=10;
Height+=10;
}
这篇关于如何在动态添加新控件时自动调整winform的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!