0

I have a numeric up down, and I want it to either add or subtract one depending on if the up or down arrow is pressed. I have the code below, but it only works to subtract one from the variable.

  Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
        If ComboBox1.SelectedIndex = 0 Then
            seatsA = seatsA - 1
            TextBox2.Text = seatsA
        ElseIf ComboBox1.SelectedIndex = 1 Then
            seatsB = seatsB - 1
            TextBox2.Text = seatsB
        ElseIf ComboBox1.SelectedIndex = 2 Then
            seatsC = seatsC - 1
            TextBox2.Text = seatsC
        End If
End Sub

Edit: If the numeric updown value is changed, the variable stores this change, each comboBox has its own variable as it needs to store the value for each. I.e., if seatsA is 20, when the user goes back to selected index one 20 is shown.

Seats start with a number...such as 75, when the numeric updown is increased, one is taken off the seats value for each seat (a,b,c)

Thanks

10
  • 1
    What are you asking, do you need the code to add one? Commented Dec 23, 2009 at 20:18
  • Yep I need to know if the numeric updown is incremented by one or deceased, and to do the same on the varible Commented Dec 23, 2009 at 20:19
  • 1
    To add a number to a variable, use the + operator. Commented Dec 23, 2009 at 20:20
  • 1
    Why don't you just check what is the value of NumericUpDown1? Commented Dec 23, 2009 at 20:21
  • 1
    Maybe you should reformulate your question because it's not clear at all. Commented Dec 23, 2009 at 20:25

1 Answer 1

2

Do you want TextBox2 to have the same value as NumericUpDown1? If it's the case, you can simply do this:

Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
        TextBox2.Text = NumericUpDown1.Value
End Sub

Edit:

From what I understand of your edit, what you want to do here is set the correct value to your NumericUpDown when the selected index changes. You can do something like this:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        If ComboBox1.SelectedIndex = 0 Then
            NumericUpDown1.Value = seatsA
        ElseIf ComboBox1.SelectedIndex = 1 Then
            NumericUpDown1.Value = seatsB
        End If
End Sub

Then to save a value change, you could do something like:

Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
        If ComboBox1.SelectedIndex = 0 Then
            seatsA = NumericUpDown1.Value
        ElseIf ComboBox1.SelectedIndex = 1 Then
            seatsB = NumericUpDown1.Value
        End If
End Sub

Other edit:

OK... I understand what you're trying to do now...

I can think of two strategies:

  1. In your form, have a LastNumericUpDownValue member in which you keep the last value of the numericupdown. Then you compare the current value to the last value, and you'll know if the value has been incremented or decremented.

  2. Keep the original number of seats as a member of the form when you load the form. Then when the NumericUpDownValue1 changes you can calculate that seatA = originalNumberOfSeats - seatsRequired (the value of NumericUpDown1)

Sign up to request clarification or add additional context in comments.

5 Comments

thanks but it needs to show a different value if the selected index on the ComboBox is changed, so if its index 1 then the numeric up down would show seatsB value.
no need to do a selectedindex check. just TextBox2.Text = NumericUpDown1.Value
seatsA etc have a value to begin with so the minus or plus one needs to be taken off this. Sorry if I wasnt clear.
@Elliot: That's why when the selected index changes I set the value corresponding to seatA or seatB or whatever ;-) You should also set the correct value to the NumericUpDown when the form loads.
NumeicUpDown should be zero when it loads...because its the amount of seats required, the seats remaining is the seatA etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.