VB.NET帮助切片使用不同的数字

时间:2015-04-02 11:53:48

标签: vb.net

我需要的是:( A,B,C,D等......是字符(节的名称))

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

我有一个标签,组合框和按钮。

1 个答案:

答案 0 :(得分: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  

除法结果需要截断为Integer,我正在使用Int()。