我正在学习使用" VISUAL BASIC 6.0"进行编程。我希望我的label1.caption值相对于textbox1.caption中的值更改...我正在使用的代码如下,但它仍然不起作用。请帮帮我。问候。
If Val(Text1) > 0 And Val(Text1) < 39 Then
Label20.Caption = "0"
Else
If Val(Text1) > 40 And Val(Text1) < 44 Then
Label20.Caption = "1"
Else
If Val(Text1) > 45 And Val(Text1) < 49 Then
Label20.Caption = "2"
Else
If Val(Text1) > 50 And Val(Text1) < 59 Then
Label20.Caption = "3"
Else
If Val(Text1) > 60 And Val(Text1) < 69 Then
Label20.Caption = "4"
Else
If Val(Text1) > 70 And Val(Text1) < 100 Then
Label20.Caption = "5"
End If
答案 0 :(得分:0)
0,39,40,44,45,49,50,59,60,69,70和100不会在上面开火。使用<=
或>=
。
答案 1 :(得分:0)
我假设您在Text1_Change事件中使用该代码?如果没有,你应该: - )
这是一个文字项目,我认为是你的意思:
'1 form with
' 1 textbox : name=Text1
' 1 label : name=Label1
Option Explicit
Private Sub Text1_Change()
Dim lngVal As Long
lngVal = Val(Text1.Text)
Select Case lngVal
Case 0 To 39
Label1.Caption = "0"
Case 40 To 44
Label1.Caption = "1"
Case 45 To 49
Label1.Caption = "2"
Case 50 To 59
Label1.Caption = "3"
Case 60 To 69
Label1.Caption = "4"
Case 70 To 100
Label1.Caption = "5"
Case Else 'clear label to show something unexpected was entered
Label1.Caption = ""
End Select
End Sub
我猜文本框应该只包含数字?要确保它只能接收数字,您可以添加以下内容:
Private Sub Text1_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKeyBack 'allow the backspace
Case vbKey0 To vbKey9 'allow keys 0 to 9
Case Else 'dont accept any other input
KeyAscii = 0
End Select
End Sub
其他一些提示: