我想知道是否有人可以帮助我改进我的代码(?)
三周前,我决定学习 C#/C++(决定从 c# 开始)非常有用,我正在尽我所能,但我在理解一些基础知识方面遇到了问题——例如数组。
我想通过单击按钮添加“x”文本框(其中“x”是 numericUpDown 的值)。
我找到了如何做到这一点的解决方案,但我有一种感觉,可以用不同的(更好的)方式编写它(我假设高级程序员会使用列表或数组)。
如果我错了,请原谅我,正如我之前提到的 - 我是新手,正在努力学习。
这是我的代码:
private void button1_Click(object sender, EventArgs e)
{
if (numericUpDown1.Value == 1)
{
txtbx1.AutoSize = true;
Controls.Add(txtbx1);
txtbx1.Location = new Point(70, 100);
}
else if (numericUpDown1.Value == 2)
{
txtbx1.AutoSize = true;
Controls.Add(txtbx1);
txtbx1.Location = new Point(70, 100);
txtbx2.AutoSize = true;
Controls.Add(txtbx2);
txtbx2.Location = new Point(70, 130);
}
else if (numericUpDown1.Value == 3)
{
txtbx1.AutoSize = true;
Controls.Add(txtbx1);
txtbx1.Location = new Point(70, 100);
txtbx2.AutoSize = true;
Controls.Add(txtbx2);
txtbx2.Location = new Point(70, 130);
txtx3.AutoSize = true;
Controls.Add(txtbx3);
txtbx3.Location = new Point(70, 160);
}
}