VB.NET帮助与不同数字

VB.NET帮助与不同数字

问题描述:

切片我需要的是这样的:(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 

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

这是因为当限制提高时,当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