我想从通过for循环生成的NumericUpDown中获取值,该值将被迭代N次。但是,在我的代码中,我只能获取第一个NumericUpDown值,并且该值是通过单击按钮触发的。

用于显示NumericUpDown工具的代码:

for (int i = 0; i < 6; i++) // should be i < n
{
    NumericUpDown note = new NumericUpDown();
    note.Name = "Note" + i.ToString();
    note.Location = new System.Drawing.Point(20, 40 + (40 * i));
    note.Size = new System.Drawing.Size(40, 25);
    note.Maximum = new decimal(new int[] { 10, 0, 0, 0 });
    this.Controls.Add(note);
}


获取值的代码:

var numericUpDown = this.Controls["note0"] as NumericUpDown;
var value = numericUpDown.Value;
MessageBox.Show(value.ToString());


如何获得所有值?非常感谢您的帮助。

最佳答案

我希望您正在寻找这样的东西:

foreach (NumericUpDown ctlNumeric in this.Controls.OfType<NumericUpDown>())
{
    var value = ctlNumeric.Value;
    MessageBox.Show(value.ToString());
}

关于c# - 如何在C#Windows窗体中获取所有动态numericupdown值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38840981/

10-13 03:16