注册 登录
编程论坛 VB6论坛

VB初学者请教关于复选框的问题,我的代码错了哪里,为什么运行不出结果

语成峰 发布于 2013-04-27 17:43, 515 次点击
Private Sub Check1_Click()
If Check1.Value = 1 Then
a = Val(Text1.Text)
Else
a = 0
End If
End Sub

Private Sub Check2_Click()
If Check2.Value = 1 Then
b = Val(Text2.Text)
Else
b = 0
End If
End Sub

Private Sub Check3_Click()
Dim s  As Integer
If Check3.Value = 1 Then
c = Val(Text3.Text)
Else
c = 0
End If
End Sub

Private Sub Check4_Click()
If Check4.Value = 1 Then
d = Val(Text4.Text)
Else
d = 0
End If
End Sub

Private Sub Check5_Click()
If Check1.Value = 1 Then
e = Val(Text5.Text)
Else
e = 0
End If
End Sub

Private Sub Command1_Click()
s = a + b + c + d + e
Text6.Text = s
End Sub

 

Private Sub Text1_LostFocus()

If a < 0 Or a > 100 Then
Text1.Text = ""
Text1.SetFocus
Else
a = Val(Text1.Text)
Text2.SetFocus

End If
End Sub
 

Private Sub Text2_LostFocus()

If b < 0 Or b > 100 Then
Text2.Text = ""
Text2.SetFocus
Else
b = Val(Text2.Text)
Text3.SetFocus
End If
End Sub

 

Private Sub Text3_LostFocus()

If c < 0 Or c > 100 Then
Text3.Text = ""
Text3.SetFocus
Else
c = Val(Text3.Text)
Text4.SetFocus
End If
End Sub

Private Sub Text4_LostFocus()

If d < 0 Or d > 100 Then
Text4.Text = ""
Text4.SetFocus
Else
d = Val(Text4.Text)
Text5.SetFocus
End If
End Sub

Private Sub Text5_LostFocus()

If e < 0 Or e > 100 Then
Text5.Text = ""
Text5.SetFocus
Else
e = Val(Text5.Text)
End If
End Sub

Private Sub Text6_Change()

End Sub
2 回复
#2
风吹过b2013-04-27 21:32
If Check1.Value = 1 Then
a = Val(Text1.Text)
Else
a = 0
End If

这一堆代码,包括下面 b,c 等的,都放到
Private Sub Command1_Click()
这里执行吧.

复选框,一般情况下,主要用来判断选项,而不响应事件.
除非有其它限定条件,如最多只能复选几个.否则都不应该在事件里写代码.
或者特殊用法.如选定显示另外一个控件出来之类.
#3
语成峰2013-04-27 23:18
回复 2楼 风吹过b
对了,我还以为只会执行第一个条件呢
1