Private Sub text1_KeyPress(KeyAscii As Integer) If KeyAscii <> vbKeyBack And KeyAscii < 48 Or KeyAscii > 57 And KeyAscii <> 13 Then KeyAscii = 0 End If End SubPrivate Sub text1_LostFocus() if trim(text1.text)<>"" then if len(trim(text1.text))<6 then msgbox"我的号码必须大于5位数!",48,"提示" text1.setfocus end if end if End Sub鼠标经过标签时显示手行:(手型图标需要自己找) private sub Form_Load() label1.MousePointer=99 '自定义鼠标形状 label1.MouseIcon=LoadPicture("D:\手型图标.ico") end sub Private Sub Label4_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) label1.MousePointer=99 End Sub
把那个backspace(删除键盘的KEYCODE屏蔽掉啊) 在你的程序外面屏蔽(在KEYDOWN事件中) if keycode=46 or keycode=8 then exit sub endif
给楼主一段我正在项目中用的数字输入方法: (你把长度自己控制一下为5位吧) Private gNumbers As StringPrivate Sub Form_Load() gNumbers = "1234567890" + Chr(8) + Chr(45) + Chr(46) '控制TextBox框只能输入数End Sub Private Sub TxtCKS_KeyPress(KeyAscii As Integer) If InStr(gNumbers, Chr(KeyAscii)) = 0 Then KeyAscii = 0 If KeyAscii = 46 And InStr(TxtCKS.Text, ".") <> 0 Then KeyAscii = 0 '"."点号只能出现一次 If KeyAscii = 45 Then '"-"号必须放在第一位,且只能出现一次 If InStr(TxtCKS.Text, "-") <> 0 Then KeyAscii = 0 Else TxtCKS.SelStart = 0 End If End If End Sub
边看边学:)Const xStr As String = "0123456789"Private Sub Text1_KeyPress(KeyAscii As Integer) '只能输入数字 KeyAscii = IIf(InStr(xStr, Chr(KeyAscii)) = 0, 0, KeyAscii) End SubPrivate Sub Text2_KeyPress(KeyAscii As Integer) '只能输入数字以外的字符 KeyAscii = IIf(InStr(xStr, Chr(KeyAscii)) = 0, KeyAscii, 0) End Sub
If IsNotChar(Types) Then If IsInt(Types) Then If KeyAscii = 8 Or KeyAscii >= 48 And KeyAscii <= 57 Then Else KeyAscii = 0 End If ElseIf IsDbl(Types) Then If KeyAscii >= 48 And KeyAscii <= 57 Then Else If KeyAscii = 8 Or KeyAscii = 45 Or KeyAscii = 46 Then Else KeyAscii = 0
End If End If ElseIf IsDateTime(Types) Then If KeyAscii >= 48 And KeyAscii <= 57 Then Else If KeyAscii = 8 Or KeyAscii = 45 Or KeyAscii = 58 Then Else KeyAscii = 0
If KeyAscii <> vbKeyBack And KeyAscii < 48 Or KeyAscii > 57 And KeyAscii <> 13 Then
KeyAscii = 0
End If
End SubPrivate Sub text1_LostFocus()
if trim(text1.text)<>"" then
if len(trim(text1.text))<6 then
msgbox"我的号码必须大于5位数!",48,"提示"
text1.setfocus
end if
end if
End Sub鼠标经过标签时显示手行:(手型图标需要自己找)
private sub Form_Load()
label1.MousePointer=99 '自定义鼠标形状
label1.MouseIcon=LoadPicture("D:\手型图标.ico")
end sub
Private Sub Label4_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
label1.MousePointer=99
End Sub
在txtbox中输入数字后。发现输入错误。想纠正。但光标去到最后。怎样才能从后向前删除?现在只能将光标移上前一个数,才能删除后一个。举例:输入 “0023”后,光标去到最后,发现“3”错误。想改“4”,想从后向前删除,但不能。只能将光标移到“2”后面,再删除“3”,非常麻烦。所以请大家指教
在你的程序外面屏蔽(在KEYDOWN事件中)
if keycode=46 or keycode=8 then
exit sub
endif
(你把长度自己控制一下为5位吧)
Private gNumbers As StringPrivate Sub Form_Load()
gNumbers = "1234567890" + Chr(8) + Chr(45) + Chr(46) '控制TextBox框只能输入数End Sub
Private Sub TxtCKS_KeyPress(KeyAscii As Integer)
If InStr(gNumbers, Chr(KeyAscii)) = 0 Then KeyAscii = 0
If KeyAscii = 46 And InStr(TxtCKS.Text, ".") <> 0 Then KeyAscii = 0 '"."点号只能出现一次
If KeyAscii = 45 Then '"-"号必须放在第一位,且只能出现一次
If InStr(TxtCKS.Text, "-") <> 0 Then
KeyAscii = 0
Else
TxtCKS.SelStart = 0
End If
End If
End Sub
2,我添加记录到SQL,只能按顺序添加,不能跳跃添加。举例: 编码 0001 姓名 张三 ,规定下一条记录是 编码 0002 姓名 李四, 不能出现一下子编码序号跳到 编码 0022 姓名 李四。敬请各位师兄指教
他的只能输数字,你的是不能输数字,反过来不就行了,即如果用户输入的字符在你禁用的范围内,就让kayascii=0就行了
'只能输入数字
KeyAscii = IIf(InStr(xStr, Chr(KeyAscii)) = 0, 0, KeyAscii)
End SubPrivate Sub Text2_KeyPress(KeyAscii As Integer)
'只能输入数字以外的字符
KeyAscii = IIf(InStr(xStr, Chr(KeyAscii)) = 0, KeyAscii, 0)
End Sub
If IsInt(Types) Then
If KeyAscii = 8 Or KeyAscii >= 48 And KeyAscii <= 57 Then
Else
KeyAscii = 0
End If
ElseIf IsDbl(Types) Then
If KeyAscii >= 48 And KeyAscii <= 57 Then
Else
If KeyAscii = 8 Or KeyAscii = 45 Or KeyAscii = 46 Then
Else
KeyAscii = 0
End If
End If
ElseIf IsDateTime(Types) Then
If KeyAscii >= 48 And KeyAscii <= 57 Then
Else
If KeyAscii = 8 Or KeyAscii = 45 Or KeyAscii = 58 Then
Else
KeyAscii = 0
End If
End If
End If
End If
你的代码虽短,可惜浪费资源,此时你要进行二次的运算。.............奉劝大家以后写代码不要为了精简而大大的浪费系统资源。汗.......等你们写个大系统时你就会知道系统资源是多么可贵的。:)
If Not (IsNumeric(Chr(KeyAscii)) Or KeyAscii = 8) Then KeyAscii = 0
End Sub
一行代码够了
这个事件是VB6开始特地加上的,为的就是让你验证值是否符合要求。Validate事件比LostFocus好,因为可以控制哪些控件出发验证事件,这样就可以让用户有机会查看一些帮助信息(否则用户无法离开该控件)