2

I want to get the values from the NumericUpDown that is generated through the for loop which will be iterated N times. However in my code, I can only get the first NumericUpDown value and it is triggered through a button click.

Code for displaying the NumericUpDown tools:

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);
}

Code for getting the values:

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

How can I get all the values? Thank you so much for all your help.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Isabella
  • 455
  • 1
  • 10
  • 23

1 Answers1

4

I hope that you are looking for something like this:

foreach (NumericUpDown ctlNumeric in this.Controls.OfType<NumericUpDown>())
{
    var value = ctlNumeric.Value;
    MessageBox.Show(value.ToString());
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • Wow! THANK YOU SO MUCH - just the way I want it! Sorry I took so long to reply! – Isabella Aug 09 '16 at 13:36
  • Its ok, glad to hear that it helped. Feel free to contact, if you need clarification or in case of doubts – sujith karivelil Aug 09 '16 at 13:37
  • Hi. I was wondering if you could help me out with the problem related to your answer in this post... http://stackoverflow.com/questions/39166868/how-to-get-both-dynamic-numericupdown-and-textbox-values-in-c-sharp-windows-form?noredirect=1#comment65676202_39166868 – Isabella Aug 26 '16 at 13:12