2014-01-20 56 views
0

我有一個5個NUD控件的組合框。我試圖弄清楚是否有辦法限制所有NUD的TOTAL值合併,並設置一個限制,以使它們所需要的最小數量合在一起。我檢查了我的書上學,似乎無法在那裏找到它,也沒有谷歌給我提供我尋求的答案。這不是我項目的必要條件,我只是覺得這個額外的接觸會很好。任何幫助,將不勝感激。謝謝。VB.NET限制多個NUD

編輯:

我使用數組,但沒有得到我想要的結果,仍在尋找四處打聽的人不能夠增加說NUD的達到極限時的值的方式嘗試。這是我目前正在做的,只是不喜歡它處理的方式,我寧願他們不要按下按鈕來查明總數是否超過了要求。

 If numForest.Value + numFountain.Value + numConstruct.Value + numIntercept.Value + numWaterFall.Value > 30 Then 
     MessageBox.Show("Total photo prints needs to be below 30") 
    Else : lstPhotoOrder.Items.Add(txtOrderName.Text) 
+1

是有一種方法:其所謂的編寫代碼。使用「Enter」和「ValueChanged」事件 – Plutonix

回答

1

由於Plutonix暗示,你可以使用ValueChanged事件審查的NumericUpDown控件的當前值。問題是,它不會告訴你以前的值爲退回到,所以儘量在字典中存儲這些值:

Private oldNums As New Dictionary(Of NumericUpDown, Decimal) 
Private revertingValue As Boolean = False 

Private Sub num_ValueChanged(sender As Object, e As EventArgs) Handles _ 
             numForest.ValueChanged, _ 
             numFountain.ValueChanged, _ 
             numConstruct.ValueChanged, _ 
             numIntercept.ValueChanged, _ 
             numWaterFall.ValueChanged 
    If Not revertingValue Then 
    Dim numControl As NumericUpDown = sender 
    If Not oldNums.ContainsKey(numControl) Then 
     oldNums.Add(numControl, numControl.Minimum) 
    End If 

    If numForest.Value + numFountain.Value + numConstruct.Value + _ 
     numIntercept.Value + numWaterFall.Value > 30 Then 
     MessageBox.Show("Total photo prints needs to be below 30") 
     revertingValue = True 
     Try 
     numControl.Value = oldNums(numControl) 
     Catch ex As Exception 
     Finally 
     revertingValue = False 
     End Try 
     numControl.Select() 
    Else 
     oldNums(numControl) = numControl.Value 
    End If 
    End If 
End Sub