【问题标题】:How to get all dynamic numericupdown values in c# windows forms如何在 c# windows 窗体中获取所有动态 numericupdown 值
【发布时间】:2016-12-14 22:10:11
【问题描述】:

我想从通过 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());

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

【问题讨论】:

  • 我在下面添加了一个答案,请告诉我这有帮助吗?

标签: c# .net loops iteration numericupdown


【解决方案1】:

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

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

【讨论】:

  • 哇!非常感谢 - 正是我想要的方式!抱歉,我花了这么长时间才回复!
  • 没关系,很高兴听到它有帮助。如果您需要澄清或有疑问,请随时联系
  • 嗨。我想知道您是否可以帮助我解决与您在这篇文章中的答案相关的问题...stackoverflow.com/questions/39166868/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-04
相关资源
最近更新 更多