2009-12-23 63 views
0

我有一個數字向上,我希望它可以添加或減去一個取決於如果向上或向下箭頭被按下。我有下面的代碼,但它只能用於從變量中減去一個。numeric updown vb.net

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 

編輯:如果數字沿着上下值發生改變時,變量存儲這種變化,每個comboBox都有自己的變量,因爲它需要存儲每個值。即,如果seatsA是20,則當用戶返回到所選索引時,顯示一個20。

座椅開始一個號碼...例如75,當數字可逆增加時,一個被取出的座椅值每個座位(A,B,C)

由於

+1

你在問,你需要在代碼中添加一個? – 2009-12-23 20:18:17

+0

是的,我需要知道數字上傳是否增加1或已故,並在變量上做相同 – Elliott 2009-12-23 20:19:36

+1

要向變量添加數字,請使用「+」運算符。 – 2009-12-23 20:20:24

回答

2

待辦事項你希望TextBox2具有與NumericUpDown1相同的值嗎?如果是這種情況,你可以簡單地這樣做:

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

編輯:

從我瞭解你的編輯,你想在這裏做什麼是正確的值設置爲您的NumericUpDown當選定的索引更改。你可以做這樣的事情:

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 

然後保存價值的變化,你可以這樣做:

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 

其他編輯:

OK ......我明白了什麼你正在嘗試做...

我可以想到兩種策略:

  1. 在您的表單中,有一個LastNumericUpDownValue成員,您可以在其中保留numericupdown的最後一個值。然後,您將當前值與最後一個值進行比較,您會知道該值是否已遞增或遞減。

  2. 當您加載表單時,請保留作爲表單成員的原始座位數。然後,當NumericUpDownValue1變化就可以計算出seatA = originalNumberOfSeats - seatsRequired(NumericUpDown1中的值)

+0

謝謝,但它需要顯示一個不同的值,如果在ComboBox上選定的索引更改,所以如果它的索引1,然後數字上下會顯示seatsB值。 – Elliott 2009-12-23 20:25:49

+0

不需要做'selectedindex'檢查。只是'TextBox2.Text = NumericUpDown1.Value' – Jason 2009-12-23 20:27:14

+0

seatsA等有一個值開始,所以減號或加號需要取消。對不起,如果我不清楚。 – Elliott 2009-12-23 20:38:57