vb select case 为什么只执行前面条件,后面就不执行了

Private Sub Text2_Change()
x = Val(Text2.Text)
Select Case x
Case Is <= 50
Label64.Caption = 5
Case Is > 40, Is < 50
Label64.Caption = 4
Case Is >= 30, Is < 40
Label64.Caption = 3
Case Is >= 20, Is < 30
Label64.Caption = 2
Case Is > 0, Is < 20
Label64.Caption = 0
End Select
End Sub
我在text2中输入小于40的数时 label64中显示的一直是5
输入大于40的数是显示的一直是4
求各位大神指点,是哪里出了问题?该怎么改?

概念误解!
Select Case 语句称为多分支语句;
对于:
Case 1
.....
Case 2
.....
是从上到下,一一进行判断的,一旦满足条件,执行后,就不会去进行下面的Case 判断了.
所以使用的时候要注意,
现在分析你的代码:
Select Case x
Case Is <= 50
Label64.Caption = 5
Case Is > 40, Is < 50
Label64.Caption = 4
Case Is >= 30, Is < 40
Label64.Caption = 3
Case Is >= 20, Is < 30
Label64.Caption = 2
Case Is > 0, Is < 20
Label64.Caption = 0
End Select
只要 x 满足 小于等于50,不管是0---50,都执行第一个Case,得到 5
后面的分支,形同虚设.
要这样从小到大排列:
x = Val(Text1.Text)
Select Case x
Case 0
Label1.Caption = ""
Case Is < 20
Label1.Caption = 0
Case Is < 30
Label1.Caption = 2
Case Is < 40
Label1.Caption = 3
Case Is < 50
Label1.Caption = 4
Case Is >= 50
Label1.Caption = 5
End Select
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-06-10
select事件条件用逗号隔开表示两个条件符合任一个都执行,所以
应该改为:case 40 to 50
case 30 to 40
case 20 to 30
case 0 to 20
第2个回答  2013-06-10
Case Is >= 20, Is < 30 这种句同 case 3 ,5 ,3 相似,是 或的关系,
这句相当于 x> =20 or x < 30 ,实际上什么数都符合条件。比尔盖茨也真是的,要是直接可以把逻辑表达式加进去就好了。
其实这种分段选择有一个函数可用,可大大简化:
if x>0 and x<=50 then
Label64.caption=switch(x<20,0,x<30,2,x<40,3,x<50,4,true,5)
end if
相似回答