Reputation: 3864
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
Upvotes: 0
Views: 3124
Reputation: 17845
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:
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.
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)
Upvotes: 2