【发布时间】:2018-07-26 03:06:15
【问题描述】:
我有一个代码,它在运行时创建一个文本框,并调整和移动预先创建的控件。 我面临的问题是我无法在运行时调整或移动我创建的控件。
这是代码。
public System.Windows.Forms.TextBox AddNewTextBox()
{
System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
this.Controls.Add(txt);
txt.Top = cLeft * 25;
txt.Left = 100;
txt.Text = "TextBox " + this.cLeft.ToString();
cLeft = cLeft + 1;
return txt;
}
private void button3_Click_1(object sender, EventArgs e)
{
AddNewTextBox();
}
private void button3_Click(object sender, EventArgs e)
{
ControlMoverOrResizer.Init(textBox1);
cboWorkType.SelectedIndex = 0;
}
我面临的问题是我不知道如何在
中引用新创建的文本框ControlMoverOrResizer.Init(textBox1);
我试着打电话
txt.文本
但它抛出的错误 无法将 String 转换为 Windows.form.controls。
请指导我在哪里犯错。 谢谢
【问题讨论】:
-
考虑到您的代码创建了多个控件,您想要移动哪个控件并不是很明显。通过在
List<TextBox>中跟踪它们来开始修复它。或者考虑一个 DataGridView。
标签: c# winforms dynamic controls