如何使两个text都符合要求后再执行各自语句

wxl900306 2013-03-09 11:17:50
下面的代码我是想要在按键按下后判断text3和text4都符合要求后在开始进行发送和延时,但语句好像不对
Private Sub Command2_Click()            '发送序列号按键
Dim outbyte1(0) As Byte
Dim number1 As Integer
Dim x As Integer, y As Integer

Command2.Enabled = False
Command3.Enabled = False



'While x = 0
If IsNumeric(Text3.Text) Then
If IsNumeric(Text4.Text) Then
If Val(Text4) < 1 Then
'x = 0
MsgBox "定时时间输入错误!", vbExclamation, "警告"
Text4 = ""
Command2.Enabled = True
End If
Else
'x = 0
MsgBox "定时时间输入错误!", vbExclamation, "警告"
Text4 = ""
Command2.Enabled = True
End If
If 0 < Val(Text3.Text) And Val(Text3.Text) < 100 Then 'text3.text是字符串,要用val转换,并且在vb中不能写成0 < Val(Text3.Text)<100要分开写
number1 = Text3.Text
outbyte1(0) = CByte(number1)
MSComm1.Output = outbyte1 '发送给序列号
Else
'x = 0
Text3.Text = ""
MsgBox "请输入正确的序列号!", vbOKOnly, "警告"
End If

Else
'x = 0
Text3.Text = ""
MsgBox "请输入正确的序列号!", vbOKOnly, "警告"
End If
'Wend
Timer1.Enabled = True
End Sub
...全文
646 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
wxl900306 2013-03-11
  • 打赏
  • 举报
回复
我自己改出来了,但还是要谢谢你们 Private Sub Command2_Click() '发送序列号按键 Dim outbyte1(0) As Byte Dim number1 As Integer Dim x As Integer, y As Integer x = 1 Command2.Enabled = False '使发送序号按键隐藏 Command3.Enabled = False '使统计按键隐藏 While x = 1 If IsNumeric(Text3.Text) And IsNumeric(Text4.Text) Then If 0 < Val(Text3.Text) And Val(Text3.Text) < 100 And Val(Text4) > 0 Then 'text3.text是字符串,要用val转换,并且在vb中不能写成0 < Val(Text3.Text)<100要分开写 number1 = Text3.Text outbyte1(0) = CByte(number1) MSComm1.Output = outbyte1 '发送给序列号 u = Text4.Text x = 2 q = 1 Else Text3.Text = "1" Text4.Text = "10" Command2.Enabled = True MsgBox "请输入正确的数值!", vbOKOnly, "警告" End If Else Text3.Text = "1" Text4.Text = "10" Command2.Enabled = True MsgBox "请输入正确的数值!", vbOKOnly, "警告" Exit Sub End If Wend Timer1.Enabled = True End Sub
熊孩子开学喽 2013-03-11
  • 打赏
  • 举报
回复
直接在text控件的lostfocus事件中判断内容是否符合要求, 然后可以把判断结果写在tag属性里面,比如: sub text1_lostfocus() text1.tag=isnumeric(text1.text) if not text1.tag then msxbox"please input a munber" end sub ... 旧酱紫,后面的判断只需要写成: if text1.tag and text2.tag then..... 这样的代码多漂亮啊
wxl900306 2013-03-09
  • 打赏
  • 举报
回复
也就是说text3的要求是:在1-99的数字,text4的要求是:>0的延时时间这两个同时满足要求后才能执行发送和延时
酷心 2013-03-09
  • 打赏
  • 举报
回复

  If (Not IsNumeric(Text3.Text)) Or (Val(Text3.Text) < 1) Or (Val(Text3.Text) > 99) Then
    MsgBox "请输入正确的序列号!", vbOKOnly, "警告"
    Text3.Text = ""
    Text3.SetFocus
    Exit Sub
  End If
  If (Not IsNumeric(Text4.Text)) Or (Val(Text4.Text) < 1) Then
    MsgBox "定时时间输入错误!", vbExclamation, "警告"
    Text4.Text = ""
    Text4.SetFocus
    Exit Sub
  End If
  '你需要执行的代码

7,789

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧