有劳各位----关于字符输入的N个小问题???

bluebill 2001-12-24 01:13:46
在开发一个小的管理系统,在字符输入方面遇到几个小问题:
1.在一个执行搜索的FORM窗口中,在由用户输入的搜索条件文本框(text1.text)中,我向控制用户只能输入输入数字,如果输入字符后点搜索按钮就报警。或者之允许用户输入字符,如果输入数字,就会报警。
2.在一个输入日期的text2.text中,我向格式化用户输入的日期值,比如用户输入“19911001"时,自动转化为”1991-10-10“.最好能把连接符号自动加入。或者在输入前显示”####-##-##“,当用户输入是自动在分隔符号间移动。
应为我是个新手,所以请大家尽量回答的详细一点,非常感谢!
...全文
118 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
feifeilonglong 2001-12-24
  • 打赏
  • 举报
回复
可以使用isnumeric函数进行判断是否为数值
NightBreak 2001-12-24
  • 打赏
  • 举报
回复
vb中有个MASKEDIT控件专门格式化输入(如时间)。你从部件中添加它,它的使用比TEXT差不多,很简单的。
Bardo 2001-12-24
  • 打赏
  • 举报
回复
Sub Test1_KeyPress(KEYASCII as integer)
Const AlowStr="1234567890.-"
if (instr(chr(KEYASCII),ALOWSTR,1))>0 THEN
BEEP
KEYASCII=0
END IF
End Sub
bluebill 2001-12-24
  • 打赏
  • 举报
回复
在VB中有专门解决这个问题的控件吗?
bluebill 2001-12-24
  • 打赏
  • 举报
回复
多谢楼上的大哥,可是还有其他的办法吗?
dxj221 2001-12-24
  • 打赏
  • 举报
回复
very easy !! use the function:: len(string){get the string len};
then use the function :left(string,n) or right(string,n){check the string}
bluebill 2001-12-24
  • 打赏
  • 举报
回复
也非常感谢wuweijustin(菜青虫) 
bluebill 2001-12-24
  • 打赏
  • 举报
回复
多谢晓栋大哥,我一定仔细研究
fraser01 2001-12-24
  • 打赏
  • 举报
回复
bluebill(甘愿为蓝):
VB中有专门的控件用于日期的输入。名称为:
MS WINDOWS Common Controls-2 6.0 中的 DTPicker
可进行日期管理。
关于数字控制和日期格式化,我给你一段参考代码,有一点不同,但参考足够了:
'档案号的控制
Private Sub txtFileNum_KeyPress(KeyAscii As Integer)
'输入数字、回车和backspace
If Not (KeyAscii >= 48 And KeyAscii <= 57 Or KeyAscii = 13 Or KeyAscii = 8) Then KeyAscii = 0
OnEnterGotoNextControl KeyAscii
End Sub

日期处理:
Public Function Err_Check(strStartYear As String, strStartMonth As String, strStartDay As String, strEndYear As String, strEndMonth As String, strEndDay As String, Optional strErr As String = "开始") As Boolean
'变量定义
Dim StartDate As Date
Dim EndDate As Date
'错误处理

'程序处理
'处理1.1:检验是否为空
Err_Check = True
If Len(strStartYear) <> 4 Then
GetControlMsg 7102
Exit Function
End If
If Len(strEndYear) <> 4 Then
GetControlMsg 7102
Exit Function
End If
If strStartYear < 1900 Or strStartYear > 2500 Then
GetControlMsg 7103
Exit Function
End If
If strEndYear < 1900 Or strEndYear > 2500 Then
GetControlMsg 7103
Exit Function
End If
If ChkObjEmpty(CStr(strStartYear), 7002, strErr & "年份") Then Exit Function
If ChkObjEmpty(CStr(strStartMonth), 7002, strErr & "月份") Then Exit Function
If ChkObjEmpty(CStr(strStartDay), 7002, strErr & "日期") Then Exit Function
If ChkObjEmpty(CStr(strEndYear), 7002, "结束年份") Then Exit Function
If ChkObjEmpty(CStr(strEndMonth), 7002, "结束月份") Then Exit Function
If ChkObjEmpty(CStr(strEndDay), 7002, "结束日期") Then Exit Function

StartDate = DateSerial(strStartYear, strStartMonth, strStartDay)
EndDate = DateSerial(strEndYear, strEndMonth, strEndDay)
'开始月份必须在1-12中
If strStartMonth > 12 Or strStartMonth < 1 Then
GetControlMsg 7004, strErr & "月份"
Exit Function
End If
'固定月份的天数不能超出范围
If CInt(strStartDay) <> Day(StartDate) Then
GetControlMsg 7004, strErr & "日期"
Exit Function
End If

'结束月份必须在1-12中
If strEndMonth > 12 Or strEndMonth < 1 Then
GetControlMsg 7004, "结束月份"
Exit Function
End If

'固定月份的天数不能超出范围
If CInt(strEndDay) <> Day(EndDate) Then
GetControlMsg 7004, "结束日期"
Exit Function
End If

'处理1.2:检验日期的输入错误
If StartDate > EndDate Then
GetControlMsg 7101
Exit Function
End If
Err_Check = False
Exit Function
'错误信息

End Function
wuweijustin 2001-12-24
  • 打赏
  • 举报
回复
Sorry

gbProgMsg "PMPT_ERR_DATE", gsModuleName, glRtn
应该成 msgbox "日期错误"
wuweijustin 2001-12-24
  • 打赏
  • 举报
回复
试试这个
1. 只输入数字
在主模块中写入

Public Function gbIsNum(KeyAscii As Integer) As Integer
If KeyAscii < 0 Then
gbIsNum = 0
Exit Function
End If
If (KeyAscii < 32 Or KeyAscii > 126) Then
gbIsNum = KeyAscii
Exit Function
End If
If (KeyAscii < 48 Or KeyAscii > 57) Then
gbIsNum = 0
Else: gbIsNum = KeyAscii
End If
End Function

在text1的keydown事件中写入
text1_keypress keycode

在text1的keypress事件中写入
keyascii=gbisnum(keyascii)

2. 输入字符,自己去查ASCII码表,gbisnum稍微改写一下,只留下字符就行了

3. 输入日期格式,可以在输入完数字后自己转化一下,我现在的做法是把日期拆成三个文本框,每个都只限定用户输入数字,用日期的时候拚起来

日期是否合法,可用以下函数判断
Function gbToCheckStartDate(StrYear As String, StrMonth As String, StrDay As String) As Boolean
Dim StrStartDate As String

gbToCheckStartDate = True

StrStartDate = StrYear & "/" & StrMonth & "/" & StrDay

If StrStartDate < "1900" & "/" & "01" & "/" & "01" Or StrStartDate > "2200" & "/" & "01" & "/" & "01" Then
gbProgMsg "PMPT_ERR_DATE", gsModuleName, glRtn
gbToCheckStartDate = False
Exit Function
End If

If Not IsDate(StrStartDate) Then
gbProgMsg "PMPT_ERR_DATE", gsModuleName, glRtn
gbToCheckStartDate = False
Exit Function
End If
End Function
bluebill 2001-12-24
  • 打赏
  • 举报
回复
VB中是不是有专门解决这个问题的控件,如果有的化有劳各位给我讲解一下具体的做法和步骤。多谢了。

7,763

社区成员

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

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