上次发了一个贴子,问了这个问题,当时没有加变量,当text1或者text2的内容为空的时候并触发lostfocus事件后无法终止(相当于死循环)!
后来一个朋友提示我加一个全局变量。现在又有问题出现了。又要麻烦大家了。谢谢!
我加了一个全局变量,在text1与text2失去焦点的不会死循环了.但是如果在text1获得焦点时,再点击其它按钮时,点击的事件不会运行.提示错误.我的代码如下.
请帮忙修改!Dim bIsErr As BooleanPrivate Sub Command1_Click()
Text3 = Text1 & Text2
End SubPrivate Sub Command2_Click()
Text1.Enabled = False
Text2.Enabled = False
Text3 = "取消"
End SubPrivate Sub Text1_GotFocus()
bIsErr = False
End SubPrivate Sub Text1_LostFocus()
If bIsErr = False And Text1 = "" Then
MsgBox "text1 不能为空"
bIsErr = True
Text1.SetFocus
End If
End SubPrivate Sub Text2_GotFocus()
bIsErr = True
End SubPrivate Sub Text2_LostFocus()
If bIsErr = False And Text2 = "" Then
MsgBox "text2 不能为空..."
bIsErr = True
Text2.SetFocus
End If
End Sub
后来一个朋友提示我加一个全局变量。现在又有问题出现了。又要麻烦大家了。谢谢!
我加了一个全局变量,在text1与text2失去焦点的不会死循环了.但是如果在text1获得焦点时,再点击其它按钮时,点击的事件不会运行.提示错误.我的代码如下.
请帮忙修改!Dim bIsErr As BooleanPrivate Sub Command1_Click()
Text3 = Text1 & Text2
End SubPrivate Sub Command2_Click()
Text1.Enabled = False
Text2.Enabled = False
Text3 = "取消"
End SubPrivate Sub Text1_GotFocus()
bIsErr = False
End SubPrivate Sub Text1_LostFocus()
If bIsErr = False And Text1 = "" Then
MsgBox "text1 不能为空"
bIsErr = True
Text1.SetFocus
End If
End SubPrivate Sub Text2_GotFocus()
bIsErr = True
End SubPrivate Sub Text2_LostFocus()
If bIsErr = False And Text2 = "" Then
MsgBox "text2 不能为空..."
bIsErr = True
Text2.SetFocus
End If
End Sub
解决方案 »
- 请教一个SQL语句。
- 在VB怎样用MS SQL2000 SERVER存储过程得到MS SQL2000 SERVER的系统日期?
- 有没有办法解决读取,ini文件大于64K,后面的读不到了
- 关于Picturebox的image属性
- 请问各位大哥,Animation控件在那里找到,说清楚点,我是菜菜,刚学vb不到两天,在线等
- ADO 改变 ACCESS当中的某一个表的字段进行操作????
- Screen.TwipsPerPixelX是什么意思
- 关于TreeView控件的一个问题,我是新手,多谢帮忙了!
- 如何释放VB调用的ActivexDLL?
- ListView简单问题2。
- 一个VB短心平台,速要,急给分,高手门看看
- 如何用VB.net2005画出这个图,代码怎么写啊?
Private Sub Text1_Validate(Cancel As Boolean)
If Text1.Text = "" Then
Call MsgBox("请输入数据")
Cancel = True
End If
End Sub
If TextValidate = False Then
Exit Sub
End If
End SubPrivate Sub Text2_LostFocus()
If Text2.Text = "" Then
Exit Sub
End If
End SubPublic Function TextValidate()
If Text1.Text = "" Then
TextValidate = False
MsgBox "text1 kong"
Text1.SetFocus
Exit Function
End If
If Text2.Text = "" Then
TextValidate = False
MsgBox "text2 kong"
Text2.SetFocus
Exit Function
End If
TextValidate = True
End Function
这样写也行
也可以把验证从LostFocus事件中移到提交事件中
Private Sub Command1_Click()
If TextValidate = False Then
Exit Sub
End If
'具体执行的代码
...
End Sub text1与text2都是空的时候和这句话放哪里没有关系
text1为空就已经退出调用的过程了
后面代码为什么还要执行啊
如果还要执行 那就是你的具体业务了 那要具体分析了
private function check() '在任何需要检查的地方调用这个函数
if len(text1.text)=0 and len(text2.text)=0 then
check=3
exit function
end if
if len(text1.text)=0 then
check=1
exit function
end if
if len(text2.text)=0 then
check=2
exit function
end if
check=0
end function
2 把你不需要触发 Validate 事件的按钮的 CausesValidate 属性设置为 False。
对,我的意思就是希望点击Command1按钮时不理会textbox是否为空。麻烦帮忙写一段代码。我按1楼的方法试了好象也不行。
谢谢!把command1.causesvalidate属性设置为false就OK了!谢谢!!!!
toolbar上的按钮如何有没有causesvalidate属性设置?
Text1.SelStart = 0
Text1.SelLength = Len(Text1)
End SubPrivate Sub Text1_LostFocus()
If Me.ActiveControl.Name = Me.Command1.Name Then Exit Sub
If Len(Text1) = 0 Then
MsgBox "文本框不能为空"
Text1.SetFocus
End If
End Sub
'是不是这样的要求呢?
现在如果那个按钮不是command.而是toolbar上的button.应该如何处理
dim ss as stringPrivate Sub Command1_Click()
set ss = 1
Text3 = Text1 & Text2
End Sub Private Sub Text1_LostFocus()
if ss = 1 then exit sub
else
If bIsErr = False And Text1 = "" Then
MsgBox "text1 不能为空"
bIsErr = True
Text1.SetFocus
End If
end if
End Sub