Loop through numericUpDown controls

Hi again,

I have a series of numericUpdown controls on my winform which I need to disable if the value is 0. I have this code.

private void DisableNums()

{

foreach (NumericUpDown numControls in amountPanel.Controls)

if (numControls.Value == 0)

{

numControls.Enabled = false;

}

}

This works fine if I only have numericUpDown controls on the amountPanel but throws an error if it comes up against any other control type. There must be a way that I can only loop through the numericUpDown controls.

Any help would be very much appreciated.

Thanks in advance



Answer this question

Loop through numericUpDown controls

  • abohmza

    Controls is simply a list of controls and by having your indexer’s type as NumericUpDown you are effectively trying to convert every single reference you receive (regardless of actual type) to a NumericUpDown... which doesn’t work too much, instead, try using an indexer of the type Control and then checking to see if you can convert it ala:

    foreach (Control control in amountPanel.Controls)

    {

    NumericUpDown numControls = control as NumericUpDown;

    if (numControls != null)

    {

    if (numControls.Value == 0)

    {

    numControls.Enabled = false;

    }

    }

    }

    In this, we iterate through getting control references and then use the as operator to try to typecast the reference into a NumericUpDown. as is pretty useful as it will return null if it cannot make the conversion, leading to the quick null check before jumping into your logic code.

    Does this work for you



  • Forrestsjs

    A ha!

    Right, I'm being a numpty again. My numericUpDown controls are within several groupBoxes contained within the amountPanel. With the code you have given I should be able to work out how to iterate through each groupbox and disable the numericUpDown controls if the value is 0.

    Thanks once again.


  • Gurjeet

    You can modify your method like this. It would check to see the type and will only go into the inner code only if its type is numericupdown. And the foreach loop will iterate through each control to avoid error which occur when type is not numericupdown.

    private void DisableNums()

    {

        foreach (Control numControls in amountPanel.Controls)

        {

               if (numControls.GetType().ToString().Equals("System.Windows.Forms.NumericUpDown"))

              {

                    if (numControls.Value == 0)

                   {

                          numControls.Enabled = false;

                   }

              }

         }

    }

     

    Thanks

    ASAD IQBAL

     



  • SriDirectX

    Hi Brendan,

    Many thanks for your quick reply and for your other comments. I can now see the error of my ways

    I have tried the code you suggested but nothing happens. I don't even get an error message.

    I set a breakpoint and stepped into the code. When I get to the if (numControls != null) if statement it jumps straight to the end of the foreach loop and over the nested if statement.

    I'm going to have a play but if you can think of anything then I'd really appreciate it.

    Thanks once again for your prompt reply


  • imrash

    I was just going to ask about something like that, glad I could help. If you need anything else feel free to add to this question or preferably post another.



  • Loop through numericUpDown controls