获取c#中动态NumericUpDown的值

本文关键字:NumericUpDown 的值 动态 获取 | 更新日期: 2023-09-27 18:04:18

我已经创建了动态NumericUpDown。我想保存用户输入的值。我怎么能那样做呢?在我保存值之后,我想对它们进行一些时间操作。知道我不知道NumericUpDown用户每次想要多少?

// To set up the location for the NumericUpDown
int xCoor;
int yCoor;
Random coor = new Random();
// Button to create the NumericUpDown
private void btnRun_Click(object sender, EventArgs e)
{
    /// I am assuming that the user choice is 8
    int value = 8;
    //this calls the method which is going to create NumericUpDown
    this.AddNewNumrical(value);
}
//Method to Create NumericUpDown
private void AddNewNumrical(int numiraclNew)
{
    for (int x = 0; x < numiraclNew; x++)
    {
        for (int y = 0; y < 1; y++)
        {
            NumericUpDown numiNumber = new NumericUpDown();
            xCoor = coor.Next(0, 700);
            yCoor = coor.Next(0, 710);
            numiNumber.Location = new Point(xCoor, yCoor);

            numiNumber.Size = new System.Drawing.Size(50, 15);
            numiNumber.Maximum = 1000;
            numiNumber.Minimum = 1;
            this.pnlNodes.Controls.Add(numiNumber);
        }
    }
}

获取c#中动态NumericUpDown的值

只需将新的NumericUpDown控件存储在另一个列表中!你也可以在Controls集合中搜索NumericUpDown控件,但你可能会捡到一些你不想要的东西:

List<NumericUpDown> numberControls = new List<NumericUpDown>();
//Method to Create NumericUpDown
private void AddNewNumrical(int numiraclNew)
{
    for (int x = 0; x < numiraclNew; x++)
    {
        NumericUpDown numiNumber = new NumericUpDown();
        xCoor = coor.Next(0, 700);
        yCoor = coor.Next(0, 710);
        numiNumber.Location = new Point(xCoor, yCoor);
        numiNumber.Size = new System.Drawing.Size(50, 15);
        numiNumber.Maximum = 1000;
        numiNumber.Minimum = 1;
        numberControls.Add(numiNumber); //Save the control off for later
        this.pnlNodes.Controls.Add(numiNumber);
    }
}

之后你可以用它做任何你想做的事情:

private void Foo()
{
    foreach (NumericUpDown userSelection in numberControls)
    {
         //Do whatever with userSelection.Value
    }
}