C# 动态添加和重新定位文本框
我有一个简单的windows表单来获取用户详细信息。如果用户希望通过单击“+”按钮添加备用手机号码,则应在其下方显示一个文本框,其他文本框应根据新添加的文本框重新定位。 我能够在运行时动态添加文本框,但无法相对于动态添加的文本框重新定位其他表单组件。下面是我的代码和表单快照。提前谢谢C# 动态添加和重新定位文本框,c#,.net,winforms,C#,.net,Winforms,我有一个简单的windows表单来获取用户详细信息。如果用户希望通过单击“+”按钮添加备用手机号码,则应在其下方显示一个文本框,其他文本框应根据新添加的文本框重新定位。 我能够在运行时动态添加文本框,但无法相对于动态添加的文本框重新定位其他表单组件。下面是我的代码和表单快照。提前谢谢 private void button1_Click(object sender, EventArgs e) { TextBox txtRun = new TextBox(); txtRun.Nam
private void button1_Click(object sender, EventArgs e)
{
TextBox txtRun = new TextBox();
txtRun.Name = "txtDynamic" + c++;
txtRun.Location = new System.Drawing.Point(90, 74 + (20 * c));
txtRun.Size = new System.Drawing.Size(200, 25);
txtRun.Location.X = 90;
txtRun.Location.Y = 74;
this.Controls.Add(txtRun);
}
尝试将所有控件(姓名、电子邮件、手机、城市等)添加到,并将其添加到窗口:
var panel = new FlowLayoutPanel() { FlowDirection = FlowDirection.TopDown }
panel.Controls.Add(namePanel);
panel.Controls.Add(emailPanel);
// etc
当用户单击+
按钮时,在所需位置插入新控件:
panel.Controls.Insert(3, newControlPanel); // add new control at index #3
如果您尚未这样做,则可能需要将每个标签
-文本框
对包装在其自己的面板
中,以便流布局按预期工作。这可以通过编程方式完成:
private void InitializeForm()
{
var layoutPanel = new FlowLayoutPanel();
// todo: initialize flow layout panel here...
layoutPanel.Controls.Add(CreatePanel("Name"));
layoutPanel.Controls.Add(CreatePanel("Email"));
// etc
this.Controls.Add(layoutPanel);
}
private Panel CreatePanel(string labelText)
{
var label = new Label(labelText);
// todo: initialize label here...
var textBox = new TextBox();
// todo: initialize textbox here...
var panel = new Panel();
panel.Controls.Add(label);
panel.Controls.Add(textBox);
// todo: initialize panel here...
return panel;
}
由于每个面板都是以完全相同的方式添加的,因此此方法还可以帮助您的表单看起来更加一致。例如,边距和填充都可以在一个位置更改。尝试将所有控件(姓名、电子邮件、手机、城市等)添加到一个位置,并将其添加到窗口:
var panel = new FlowLayoutPanel() { FlowDirection = FlowDirection.TopDown }
panel.Controls.Add(namePanel);
panel.Controls.Add(emailPanel);
// etc
当用户单击+
按钮时,在所需位置插入新控件:
panel.Controls.Insert(3, newControlPanel); // add new control at index #3
如果您尚未这样做,则可能需要将每个标签
-文本框
对包装在其自己的面板
中,以便流布局按预期工作。这可以通过编程方式完成:
private void InitializeForm()
{
var layoutPanel = new FlowLayoutPanel();
// todo: initialize flow layout panel here...
layoutPanel.Controls.Add(CreatePanel("Name"));
layoutPanel.Controls.Add(CreatePanel("Email"));
// etc
this.Controls.Add(layoutPanel);
}
private Panel CreatePanel(string labelText)
{
var label = new Label(labelText);
// todo: initialize label here...
var textBox = new TextBox();
// todo: initialize textbox here...
var panel = new Panel();
panel.Controls.Add(label);
panel.Controls.Add(textBox);
// todo: initialize panel here...
return panel;
}
由于每个面板都是以完全相同的方式添加的,因此此方法还可以帮助您的表单看起来更加一致。例如,边距和填充都可以在一个位置更改。每个
表单
对象都有它的控件
列出并存储所有对象的位置。您也正在访问它,并在此代码中添加您的TextBox
:
this.Controls.Add(txtRun);
您可以决定如何处理该案例,因为有更多的解决方案。您可以浏览所有控件,找到MobileTextBox
下面的控件,然后将它们向下移动几个像素
或者您可以将MobileTextBox
下面的控件分组到一些GroupBox
(或其他分组控件)中,然后只需移动GroupBox
和/或是@gt-FlowLayoutPanel
提到的解决方案,它将以与WPF框架
类似的方式处理布局
一些代码示例:
private void button1_Click(object sender, EventArgs e)
{
TextBox txtRun = new TextBox();
txtRun.Name = "txtDynamic" + c++;
txtRun.Location = new System.Drawing.Point(90, 74 + (20 * c));
this.Controls.Add(txtRun);
//removed some code for brevity
//1st solution
foreach(Control item in this.Controls)
{
//there can be some other condition
//based on e.g. name of TB
//or if the type is GroupBox with some name
if(item.Location.Y >= txtRun.Location.Y)
item.Location.Y = item.Location.Y + 25;
}
}
每个
表单
对象都有其控件
列出和存储所有对象的位置。您也正在访问它,并在此代码中添加您的TextBox
:
this.Controls.Add(txtRun);
您可以决定如何处理该案例,因为有更多的解决方案。您可以浏览所有控件,找到MobileTextBox
下面的控件,然后将它们向下移动几个像素
或者您可以将MobileTextBox
下面的控件分组到一些GroupBox
(或其他分组控件)中,然后只需移动GroupBox
和/或是@gt-FlowLayoutPanel
提到的解决方案,它将以与WPF框架
类似的方式处理布局
一些代码示例:
private void button1_Click(object sender, EventArgs e)
{
TextBox txtRun = new TextBox();
txtRun.Name = "txtDynamic" + c++;
txtRun.Location = new System.Drawing.Point(90, 74 + (20 * c));
this.Controls.Add(txtRun);
//removed some code for brevity
//1st solution
foreach(Control item in this.Controls)
{
//there can be some other condition
//based on e.g. name of TB
//or if the type is GroupBox with some name
if(item.Location.Y >= txtRun.Location.Y)
item.Location.Y = item.Location.Y + 25;
}
}
试试这个
int c = 0; // for uinque txtDynamic text creation
private void button1_Click(object sender, EventArgs e)
{
TextBox txtDynamic = this.Controls.Find("txtDynamic" + c, true)[0] as TextBox; // find lastly added txtDynamic
TextBox txtRun = new TextBox();
txtRun.Name = "txtDynamic" + ++c;
txtRun.Size = new System.Drawing.Size(100, 20);
txtRun.Location = new Point(txtDynamic.Location.X, txtDynamic.Location.Y + 35); // X axis will be same y will increase with count 35
foreach (Control item in this.Controls)
{
if (item.Location.Y >= txtRun.Location.Y){ // if there is an item that has greater Y location
item.Location = new Point(item.Location.X, txtRun.Location.Y + 35); // It should increase its value as 35 too.
}
this.Controls.Add(txtRun);
}
}
编辑1:
好的,我已经用拖放创建了控件,我不知道你是否通过编程创建了它们+按钮将在Mobil文本框之后添加新的文本框。所以,我拖放的移动文本框将是最重要的。所以我把它的名字命名为“txtDynamic0”
将断点放在按钮1\u单击,您的c
变量的起始点的值与0不同
结果强>
希望有帮助,试试这个
int c = 0; // for uinque txtDynamic text creation
private void button1_Click(object sender, EventArgs e)
{
TextBox txtDynamic = this.Controls.Find("txtDynamic" + c, true)[0] as TextBox; // find lastly added txtDynamic
TextBox txtRun = new TextBox();
txtRun.Name = "txtDynamic" + ++c;
txtRun.Size = new System.Drawing.Size(100, 20);
txtRun.Location = new Point(txtDynamic.Location.X, txtDynamic.Location.Y + 35); // X axis will be same y will increase with count 35
foreach (Control item in this.Controls)
{
if (item.Location.Y >= txtRun.Location.Y){ // if there is an item that has greater Y location
item.Location = new Point(item.Location.X, txtRun.Location.Y + 35); // It should increase its value as 35 too.
}
this.Controls.Add(txtRun);
}
}
编辑1:
好的,我已经用拖放创建了控件,我不知道你是否通过编程创建了它们+按钮将在Mobil文本框之后添加新的文本框。所以,我拖放的移动文本框将是最重要的。所以我把它的名字命名为“txtDynamic0”
将断点放在按钮1\u单击,您的c
变量的起始点的值与0不同
结果强>
希望有帮助,我得到的异常索引超出了arrayI给出的移动文本框Id为txtDynamic0的初始值范围。这就是为什么我使用++C而不是C++的原因。更改移动文本框的Id。这就是为什么你得到数组exception.Id的边界?你是说你的名字吗textbox@SameerBetageri对不起,您是textbox的正确名称。@berkey仍然存在相同的错误。请详细说明要执行的操作。我收到的异常索引超出了数组的界限。我将移动textbox的Id作为txtDynamic0作为初始值。这就是为什么我使用++C而不是C++的原因。更改移动文本框的Id。这就是为什么你得到数组exception.Id的边界?你是说你的名字吗textbox@SameerBetageri很抱歉,您的文本框名称是正确的。@berkey仍然有相同的错误。您能详细说明该怎么做吗