当前位置:编程学习 > VB >>

vb中 提醒text中输入必须为正整数的 代码怎么编写呀

如题 本人刚学VB 现在想知道 在一个text中输入一个数字 必须让他为正整数 不然就用msgbox提醒 怎么设计这个代码 急求~~~
答案:if int(text1.text)<>text1.text or text1.text<=0 then
msgbox "请输入正整数"
end if
其他:Private Sub Text1_KeyPress(KeyAscii As Integer) If Chr(KeyAscii) 很简单,不就是看看他的原来的数A减去取整A的数的差是否为0,结果为0是整数。
举例:
设有个输入数值的文本框Text1
Dim Num_Input as Integer
Num_Input=Val(Text1.Text)
If Num_Input<>0 then 
    If Num_Input-Int(Val(Text1.Text))<>0 then Msgbox "您输入必须的必须是整数",48,"输入提示":Text1.SetFocus 
End If 
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Chr(KeyAscii) > 9 And KeyAscii <> 8 Then KeyAscii = 0
End Sub

'VB6.0测试过关 很简单:
if instr(text1.text,".")<>0 then msgbox "必须输入整数!" Private Sub Text1_KeyPress(KeyAscii As Integer)
Debug.Print KeyAscii
  '限制输入的字符仅限于0~9、退格
  If KeyAscii < 48 Or KeyAscii > 57 Then '<0的ASCII码,或大于9
    If KeyAscii <> 8 Then '8是退格键
      KeyAscii = 0 '取消这次输入
    End If
  End If
End Sub
Private Sub Text1_Validate( Cancel As Boolean)
  '文本框失去焦点,转到下一个控件时验证输入的值是否符合要求
  If  Val(Text1(Index))=0 then
    '不符合规则的输入“0”
    MsgBox "输入不符合规则!"
    Text1.SelStart = 0
    Text1.SelLength = Len(Text1)
    Cancel = True
  End If
End Sub Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim i As Variant
Dim j As Long
i = Text1.Text
If IsNumeric(i) Then
Text1.Text = ""
MsgBox "输入不符合规则!"
Else
j = i
End If
If j >= 1 Then
Text1.Text = j
else
Text1.Text = ""
MsgBox "输入不符合规则!"
End If
End Sub
创建文本框Text1就可以了 希望能帮到你  这里Keyup绝对比KeyPress好用哈哈 

上一个:VB编程,求高手帮忙
下一个:vb 2010 中的 combobox怎样用

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,