【发布时间】:2021-09-23 00:24:46
【问题描述】:
我打算让它创建与 numericUpDown1 的值一样多的文本框,我将进行一些调整,以便它在值降低时也删除文本框,但它根本不起作用。我不太确定为什么会这样。
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
var numVal = numericUpDown1.Value;
int count = 2;
int y = 86;
for (int i = 0; i < numVal; i++)
{
//Create the dynamic TextBox.
TextBox text1 = new TextBox();
y += 20;
text1.Location = new Point(719, y);
text1.Name = "gearText_" + count;
count += 1;
}
}
【问题讨论】:
-
可能是因为您没有将新的 TextBox 控件添加到任何容器中,所以它们处于 limbo 中。 -- 我建议使用 FlowLayoutPanel 作为容器和支持集合,作为
Dictionary<int, TextBox>。 -- 请注意,要删除控件,您必须将其处理掉。要添加它,您应该将一个或多个添加到集合中,但不要在每次增加计数器时重复相同的循环(因此,您可能应该存储以前的值并使用差异)。
标签: c# winforms for-loop textbox numericupdown