簡體   English   中英

如何從動態創建的NumericUpDown獲取值?

[英]How do I get the values from a dynamically created 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);
}

使用窗體的Control集合訪問控件,並將其傳遞給numericUpDown控件的名稱:

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

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

您仍然可以使用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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM