在TEXT文本框(数组控件)中只某个控件只允许输入数字,如何实现?

3821 2001-08-13 02:08:20
在TEXT文本框(数组控件)中只某个控件只允许输入数字,如何实现?
如数据管理中的‘身份证’只能输入数字,且判断其位数如何实现?
问题难点在于‘身份证’这一项是一fields(4),从哪个判断?先谢过了!
...全文
288 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
IamSailer 2001-08-18
  • 打赏
  • 举报
回复
可以用masked edit控件嘛,干嘛要在一棵树上吊死???
QQRN 2001-08-18
  • 打赏
  • 举报
回复
o_o
Seedling 2001-08-17
  • 打赏
  • 举报
回复

Private Sub Text1_Change(Index As Integer)
If Index = 4 And Not IsNumeric(Text1(Index)) Then
If Text1(Index).Text = "" Then Exit Sub
Text1(Index).Text = Mid(Text1(Index).Text, 1, Len(Text1(Index).Text) - 1)
'MsgBox "只允许输入数字!"
Text1(Index).SelStart = Len(Text1(Index).Text)
End If
End Sub
riyueming 2001-08-17
  • 打赏
  • 举报
回复
咱也学习学习
3821 2001-08-17
  • 打赏
  • 举报
回复
谢谢诸位!!!其它如果单一一个控件,用isnumeric(非isnumric!)检查是否数字型、用len检查其长度是否符合要求就可以,不必用那么多函数(并没否定函数的作用啊^_^但VB就是要简单嘛)
问题是,通过什么事件判断控件组里某个控件的变化?但我不想触动控件组里其它控件....
我这个控件是txtfields控件组里的第4个......
ilfsm 2001-08-16
  • 打赏
  • 举报
回复
给你两个自定义函数:
Public Function CheckNum(KeyAscii As Integer) As Boolean
'---------控制数字字符输入--------------
CheckNum = False
If (KeyAscii < 48 Or KeyAscii > 57) And _
KeyAscii <> 46 And KeyAscii <> 8 Then KeyAscii = 0
CheckNum = True
End Function

Public Function IsNum(Text1 As TextBox) As Boolean
'-------判断是否为数字-----------
IsNum = False
If IsNumeric(Text1.Text) = False Then
SM "请输入正确的数字!", 0
Text1.SetFocus
Exit Function
End If
IsNum = True
End Function

第一个函数在KeyPress时件中调用(也可随意控制所要输入的字符,加入起ascii code即可),第二个函数在保存时进行数据验证(因为用户可能粘贴文本)
调用方式如:if not CheckNum(...) then exit sub
skydg 2001-08-16
  • 打赏
  • 举报
回复
我贴过的,只是没给分你们就不进来看
不知道合不合你要求,请进行相应修改即可

'限制输入字符
Function ValiText(KeyIn As Integer, ValidateString As String, Editable As Boolean) As Integer
Dim ValidateList As String
Dim KeyOut As Integer
If Editable = True Then
ValidateList = UCase(ValidateString) & Chr(8)
Else
ValidateList = UCase(ValidateString)
End If
If InStr(1, ValidateList, UCase(Chr(KeyIn)), 1) > 0 Then
KeyOut = KeyIn
Else
KeyOut = 0
Beep
End If
ValiText = KeyOut
End Function

'txtboxs事件
Private Sub txtFields_KeyPress(index As Integer, KeyAscii As Integer)
If KeyAscii = 13 And index <> 25 Then
SendKeys "{tab}"
KeyAscii = 0
End If

If index = 4 Then
KeyAscii = ValiText(KeyAscii, "0123456789", True)
End If

End Sub



保存数据时加入如下代码作为验证


'验证身份证号码

Dim strBirthday1 As String, strBirthday2 As String
Dim strSex1 As String, strSex2 As String

If Combo(2).Text = "男" Then
strSex2 = 1
Else
strSex2 = 0
End If

If txtFields(8).Text = "" Then
!公民身份证件编号 = txtFields(8).Text & ""
ElseIf Len(txtFields(8).Text) = 15 Or Len(txtFields(8).Text) = 18 Then

'15位身份证号码
If Len(txtFields(8).Text) = 15 Then

strBirthday1 = Format(txtFields(6).Text, "yymmdd") '出生日期
strBirthday2 = Mid$(txtFields(8).Text, 7, 6) '身份证日期
strSex1 = Right$(txtFields(8).Text, 1) Mod 2 '身份证性别

If strBirthday1 = strBirthday2 And strSex1 = strSex2 Then
!公民身份证件编号 = txtFields(8).Text & ""
Else
MsgBox "身份证件编号出错,请重新输入。", vbInformation
cErr = True
txtFields(8).Text = ""
txtFields(8).SetFocus
Exit Sub
End If

Else '18位身份证号码

strBirthday1 = Format(txtFields(6).Text, "yyyymmdd") '出生日期
strBirthday2 = Mid$(txtFields(8).Text, 7, 8) '身份证日期
strSex1 = Mid$(txtFields(8).Text, 17, 1) Mod 2 '身份证性别

If strBirthday1 = strBirthday2 And strSex1 = strSex2 Then
!公民身份证件编号 = txtFields(8).Text & ""
Else
MsgBox "身份证件编号出错,请重新输入。", vbInformation
cErr = True
txtFields(8).Text = ""
txtFields(8).SetFocus
Exit Sub
End If

End If

Else

Dim msg As Integer
msg = MsgBox("身份证件编号出错,按确定强行保存,按取消重新输入。", vbOKCancel + vbQuestion, "错误...")
If msg = vbCancel Then
cErr = True
txtFields(8).Text = ""
txtFields(8).SetFocus
Exit Sub
Else
!公民身份证件编号 = txtFields(8).Text & ""
End If

End If
3821 2001-08-16
  • 打赏
  • 举报
回复
谢过诸位!但问题未解决,我这个文本控件是控件组里的一个,说简单点是通过什么事件判断控件组里某个控件的变化?但我不想触动控件组里其它控件....
_liang 2001-08-14
  • 打赏
  • 举报
回复
keyascii事件中控制
lanren_me 2001-08-14
  • 打赏
  • 举报
回复
isnumber
cqq_chen 2001-08-14
  • 打赏
  • 举报
回复
你也可以用别一个叫什么MASK的控件来做啊!
不过我个人认为,如果你是要输入有效证件号,不能只限输入数字的。
我看过其它国家的身份证号就是有数字以外的东东。
playyuer 2001-08-14
  • 打赏
  • 举报
回复
精华区:
在TEXT文本框中只允许输入数字和小数点,如何实现?
http://www.csdn.net/expert/TopicView.asp?id=114714
homqom 2001-08-14
  • 打赏
  • 举报
回复
在Text1_KeyPress()事件中加
dim x%
If x>="0" and x<="9" Then
text1.text setfoucs
else
msgbox("wrong")
text1.text lostfoucs
End If
end sub
yangbin302 2001-08-13
  • 打赏
  • 举报
回复
if isnumber(text1.text) then
.........
else
exit sub
endif
zeng_zhh 2001-08-13
  • 打赏
  • 举报
回复
在Text1_KeyPress()事件中加
If ((KeyAscii > 57) Or (KeyAscii < 48)) And (KeyAscii <> 46) And (KeyAscii <> 8) And (KeyAscii <> 13) Then
KeyAscii = 27
End If
Ninputer 2001-08-13
  • 打赏
  • 举报
回复
Private Sub Text1_KeyPress(Keyascii As Integer)
If Keyascii < Asc("0") Or Keyascii > Asc("9") Then
KeyAscii = 0
End IF
gameboy999 2001-08-13
  • 打赏
  • 举报
回复
在text change里面加入下面代码,没用找我。
可以杜绝包括 ^V ^C拷贝进来的非数字,并且不会把所有数字消失
如果需要比较长的数字,把Clng部分换成isnumric判断

Private Sub Txtkey_Change()
Dim hh As Long
On Error GoTo errorhandle
hh = CLng(TxtKey.Text)
Exit Sub
errorhandle:
If Len(TxtKey.Text) <> 0 Then
TxtKey.Text = Left(TxtKey.Text, Len(TxtKey.Text) - 1)
TxtKey.SelStart = Len(TxtKey.Text)
Else
TxtKey.Text = ""
End If
End Sub

yzj_crazyman 2001-08-13
  • 打赏
  • 举报
回复
在text_lostfocus中判断,if not isnumric(text(index)) then exit sub

1,453

社区成员

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

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