給你一個自定義文本框的例子 Private Sub txtDateTimeS_GotFocus() txtDateTimeS.SelStart = 0 txtDateTimeS.SelLength = Len(txtDateTimeS.Text) End SubPrivate Sub txtDateTimeS_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = 13 Then txtDateTimeE.SetFocus End If End SubPrivate Sub txtDateTimeS_KeyPress(KeyAscii As Integer) If (KeyAscii > 57 Or KeyAscii < 48) And KeyAscii <> 46 And KeyAscii <> 8 And KeyAscii <> 13 Then txtDateTimeS.Locked = True Else txtDateTimeS.Locked = False End If If (Len(txtDateTimeS.Text) = 4 Or Len(txtDateTimeS.Text) = 7) And KeyAscii <> 8 Then txtDateTimeS.Text = txtDateTimeS.Text & "/" txtDateTimeS.SelStart = Len(txtDateTimeS.Text) End If If Len(txtDateTimeS.Text) = 10 And KeyAscii <> 8 Then txtDateTimeS.Text = txtDateTimeS.Text & " " txtDateTimeS.SelStart = Len(txtDateTimeS.Text) End If If (Len(txtDateTimeS.Text) = 13 Or Len(txtDateTimeS.Text) = 16) And KeyAscii <> 8 Then txtDateTimeS.Text = txtDateTimeS.Text & ":" txtDateTimeS.SelStart = Len(txtDateTimeS.Text) End If End SubPrivate Sub txtDateTimeS_LostFocus() If Not IsDate(txtDateTimeS.Text) And txtDateTimeS.Text <> "" Then MsgBox "不是合法日期格式" txtDateTimeS.Text = "" txtDateTimeS.SetFocus Exit Sub End If End Sub 格式為:日期時間型
"工程"/"部件"/Microsoft Windows Common Controls-2 5.0/6.0
Private Sub txtDateTimeS_GotFocus()
txtDateTimeS.SelStart = 0
txtDateTimeS.SelLength = Len(txtDateTimeS.Text)
End SubPrivate Sub txtDateTimeS_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = 13 Then
txtDateTimeE.SetFocus
End If
End SubPrivate Sub txtDateTimeS_KeyPress(KeyAscii As Integer)
If (KeyAscii > 57 Or KeyAscii < 48) And KeyAscii <> 46 And KeyAscii <> 8 And KeyAscii <> 13 Then
txtDateTimeS.Locked = True
Else
txtDateTimeS.Locked = False
End If
If (Len(txtDateTimeS.Text) = 4 Or Len(txtDateTimeS.Text) = 7) And KeyAscii <> 8 Then
txtDateTimeS.Text = txtDateTimeS.Text & "/"
txtDateTimeS.SelStart = Len(txtDateTimeS.Text)
End If
If Len(txtDateTimeS.Text) = 10 And KeyAscii <> 8 Then
txtDateTimeS.Text = txtDateTimeS.Text & " "
txtDateTimeS.SelStart = Len(txtDateTimeS.Text)
End If
If (Len(txtDateTimeS.Text) = 13 Or Len(txtDateTimeS.Text) = 16) And KeyAscii <> 8 Then
txtDateTimeS.Text = txtDateTimeS.Text & ":"
txtDateTimeS.SelStart = Len(txtDateTimeS.Text)
End If
End SubPrivate Sub txtDateTimeS_LostFocus()
If Not IsDate(txtDateTimeS.Text) And txtDateTimeS.Text <> "" Then
MsgBox "不是合法日期格式"
txtDateTimeS.Text = ""
txtDateTimeS.SetFocus
Exit Sub
End If
End Sub
格式為:日期時間型