2015-04-02 30 views
1

切片我需要的是這樣的:(A,B,C,d,等等。是字符的部分的(名))VB.NET幫助與不同數字

A = 0-40

B = 41-80

C = 81-120

d = 121-160

我有一個包含從0到1040的數字的組合框,當我選擇數字(例如80)時,該部分變成C,它應該是B,因爲它在41-80之內,如上所示。以及我選擇120,它變成D,應該是C等。只有1-40的數字完美地工作。這裏是我的代碼:

Dim counter As String 
Dim mysection As String 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    counter = cmbYearAdd.SelectedItem 
    Dim a As Char = "A" 
    Dim distinct1% = 1, distinct2% = 40 
    For x As Integer = counter To 1040 
     If x >= distinct1 And x <= distinct2 Then 
      mysection = a 
      Exit For 
     Else 
      a = Chr(Asc(a) + 1) 
      distinct1 += 40 
      distinct2 += 40 
     End If 
    Next 
    lblSectionAdd.Text = "Section: " & mysection 
End Sub 

我有一個標籤,組合框和按鈕。

回答

2

這是因爲當限制提高時,當for循環重新開始時,x的值會增加。
但請不要嘗試修復它 - 這種方法是非常尷尬的,你不應該再花功夫了。只要你的截面尺寸直接計算值屬於哪個部分保持固定的計數器值和除法:

Dim counter As String 
Dim mysection As String 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
    counter = cmbYearAdd.SelectedItem 
    Dim a As Char = "A" 
    Dim sectionsize as Integer = 40 
    Dim sect as Integer 

    sect = Int((counter-1)/sectionsize) 
    mysection = Chr(Asc("A") + sect) 
    lblSectionAdd.Text = "Section: " & mysection 
End Sub 

分工的結果需要被截斷爲整數,我使用INT()這一點。

+0

哇,它完美的作品!謝謝 – Shivr 2015-04-02 12:20:21

+0

很高興我能幫忙。我沒有VB.net的簡單說明。如果你滿意,如果你喜歡,你可以接受我的答案。 – user1016274 2015-04-02 12:31:32