vb中怎样设置textbox只能输入日期并且设置格式为yyyy-mm-dd
2个回答
展开全部
设置textbox的输入格式。
在窗体内加入控件text1,然后复制下面代码,运行即可(此功能不太完善,供参考):
Option Explicit
Private Sub Form_Load()
Text1.Text = " - - : : " '初始化格式
End Sub
Private Sub Text1_Change()
Select Case Text1.SelStart
Case 4, 7, 10, 13, 16 '位置控制
Text1.SelStart = Text1.SelStart + 1
End Select
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim i As Long
With Text1
If IsNumeric(Chr(KeyAscii)) = False Then KeyAscii = 0: Exit Sub '非数字退出
i = .SelStart
If i = 19 Then '防止数字输入超出范围
KeyAscii = 0
Exit Sub
End If
.Text = Left(.Text, i) & Mid(.Text, i + 2)
.SelStart = i
End With
End Sub
在窗体内加入控件text1,然后复制下面代码,运行即可(此功能不太完善,供参考):
Option Explicit
Private Sub Form_Load()
Text1.Text = " - - : : " '初始化格式
End Sub
Private Sub Text1_Change()
Select Case Text1.SelStart
Case 4, 7, 10, 13, 16 '位置控制
Text1.SelStart = Text1.SelStart + 1
End Select
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim i As Long
With Text1
If IsNumeric(Chr(KeyAscii)) = False Then KeyAscii = 0: Exit Sub '非数字退出
i = .SelStart
If i = 19 Then '防止数字输入超出范围
KeyAscii = 0
Exit Sub
End If
.Text = Left(.Text, i) & Mid(.Text, i + 2)
.SelStart = i
End With
End Sub
追问
谢谢,我试了一下,确实可以,但是如果输入错误用Backspace键怎么不能删除,如果要改上面的代码,要怎样改?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询