vb中能在文本框设置输入的格式吗?

来源:百度知道 编辑:UC知道 时间:2024/05/12 08:08:36
例如我想规定一个文本框是输入日期的,形式是####-##-##
当输入了其他格式时,提示必须按该格式来输入
这有可能实现吗?

if isdate(text1.text) then
else
text1.text=""
msgbox"输入格式不对"
end if

上面两个写的都有错误
第一个可以输入2000年3月
第二个你输入234看看

Private Sub Text1_LostFocus()
If Text1.Text = "" Then Exit Sub

If IsDate(Text1.Text) Then

Text1.Text = Format(Text1.Text, "yyyy-mm-dd")
Else
Text1.Text = ""
MsgBox "输入格式不对"
End If
End Sub

可以,在text控件的失去焦点事件判断就可以了!代码如下:
Private Sub Text1_LostFocus()
On Error GoTo Errorline
Text1.Text = CDate(Text1.Text)
Text1.Text = Format(Text1.Text, "yyyy-mm-dd")
Exit Sub
Errorline:
Text1.Text = ""
Text1.SetFocus
End Sub

我现编了个代码,希望对你有用:
Private Sub Command1_Click()
Dim i As Date
On Error GoTo xxx
i = Format(Text1.Text, "yyyy-mm-dd")
Exit Sub
xxx:
MsgBox "输入日期格式错误,正确格式应为yyyy-mm-dd", vbInform