如何从动态创建的NumericUpDown中获取值?

时间:2014-06-15 11:13:20

标签: c# winforms numericupdown

如何从C#中的动态NumericUpDown获取值?

这是我的代码,

for (int i=0; i<n;i++)
{
    NumericUpDown notele = new NumericUpDown();
    notele.Name = "note" + i.ToString();              
    notele.Location = new System.Drawing.Point(280, 208 + (30 * i));
    notele.Size = new System.Drawing.Size(40, 25);
    notele.BackColor = System.Drawing.SystemColors.ControlDark;
    notele.Maximum = new decimal(new int[] {10,0,0, 0});
    this.Controls.Add(notele);
}

2 个答案:

答案 0 :(得分:2)

使用表单的Control collection访问您的控件,并将numericUpDown控件的名称传递给它:

var numericUpDown  = this.Controls["note0"] as NumericUpDown;

if(numericUpDown != null)
{
   var value = numericUpDown.Value;
}

答案 1 :(得分:0)

您仍然可以使用ValueChanged事件:

...
    notelle.ValueChanged += UpDnValueChangedHandler;

    this.Controls.Add(notele);
}

private void UpDnValueChangedHandler(object sender, EventArgs e)
{
    // sender references the control which value was changed:
    NumericUpDown notelle = sender as NumericUpDown;

    // further processing goes here
}