eLigt
eLigt

Reputation: 63

numericupdown loop clicking button

I want to use buttons for incrementing and decrementing number is written numeric up down which has a boundary. For example I'd like to see something like this in loop:1 2 3 4 1 2 3 4 1 4 3 2 1 2 3 4 1 My code is down here:

int selection=1;
bool buttonIncrClickedPrevious = false;
bool buttonDecrClickedPrevious = false;

private void buttonIncr_Click(object sender, EventArgs e)
        {
            buttonIncrClickedPrevious = true;

            if (selection<= 4 && selection> 0)
            {
                numericUpDown1.Value = selection;
                if (buttonDecrClickedPrevious == true)
                {
                    selection--;
                }
                else
                {
                    selection++;
                }   

            }
            else if (selection>= 5)
            {
                selection= 1;
                numericUpDown1.Value = selection;
                selection++;
            }

        }


    private void buttonDecr_Click(object sender, EventArgs e)
        {
            buttonDecrClickedPrevious = true;

            if (selection<= 4 && selection> 0)
            {
                numericUpDown1.Value = selection;
                if (buttonIncrClickedPrevious == true)
                {
                    selection++;
                }
                else
                {
                    selection--;
                }

            }
            else if (selection<= 0)
            {
                selection= 4;
                numericUpDown1.Value = selection;
                selection--;
            }
        }

Upvotes: 0

Views: 236

Answers (2)

Frank Ball
Frank Ball

Reputation: 1126

private void buttonIncr_Click(object sender, EventArgs e)
{
            if(numericUpDown1.Value == 4)
              {
                  numericUpDown1.Value = 1;
              } 
              else
              {
                 numericUpDown1.Value++;
              }
        }


    private void buttonDecr_Click(object sender, EventArgs e)
        {
            if(numericUpDown1.Value == 1)
              {
                  numericUpDown1.Value = 4;
              } 
              else
              {
                 numericUpDown1.Value--;
              }
        }

Upvotes: 0

Alex Barber
Alex Barber

Reputation: 149

It might be simpler to use the Minimum and Maximum properties of the numeric up/down itself to restrict it to the range 0..4

There won't be a loop as you describe (1 2 3 4 --> 1 2 3 4, etc.) but by setting the Maximum your user will see that they can't set the control to a higher value than the maximum.

Upvotes: 1

Related Questions