Вопрос

I have several panels on a form that I want to appear corresponding to a numericUpDown value. (ie- panel 1 is visible when the value is 1, panels 1 and 2 are visible when the number is 2, panels 1 2 and 3 are visible when the value is 3, ect)

I am able to get the initial panel to function as expected with my existing code, but the subsequent ones are not appearing or disappearing as I intended. I'm not quite sure why. Is it because the value of the NUP is not updating when it is changed?

Code:

private void petNumNumericUpDown_ValueChanged(object sender, EventArgs e)

    {
        if ((petNumNumericUpDown.Value == 1) || (petNumNumericUpDown.Value == 2) ||(petNumNumericUpDown.Value == 3) || (petNumNumericUpDown.Value == 4) || (petNumNumericUpDown.Value == 5))
        {
            pet1Panel.Visible = true;
        }

        else
        {
            pet1Panel.Visible = false;
        }

        if((petNumNumericUpDown.Value == 2) || (petNumNumericUpDown.Value == 3) || (petNumNumericUpDown.Value == 4) || (petNumNumericUpDown.Value == 5))
        {
            pet2Panel.Visible = true;
        }

        else
        {
            pet2Panel.Visible = false;
        }
    }

I'm looking to have this continue up until 5. Any insight on what I'm doing wrong would be appreciated.

Это было полезно?

Решение

You can write simpler code to achieve your goal:

pet1Panel.Visible = (petNumNumericUpDown.Value >= 1);
pet2Panel.Visible = (petNumNumericUpDown.Value >= 2);
...
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top