4 2 第 6 章 控 件 与用户窗体 范例 6 7 文本框只能输入数值 Private Sub TextBox1_KeyPress(ByVal KeyANSI As MSForms
ReturnInteger) With TextBox1 Select Case KeyANSI Case Asc("0") To Asc("9") Case Asc("-") If InStr(1,
Text, "-") > 0 Or
SelStart > 0 Then KeyANSI = 0 End If Case Asc("
") If InStr(1,
Text, "
") > 0 Then KeyANSI = 0 Case Else KeyANSI = 0 End Select End With End Sub Private Sub TextBox1_Change() Dim i As Integer Dim Str As String With TextBox1 For i = 1 To Len(
Text) Str = Mid(
Text, i, 1) Select Case Str Case "
", "-", "0" To "9" Case Else
Text = Replace(
Text, Str, "") End Select Next End With End Sub 4 3 范例 6 8 限制文本框的输入长度 Private Sub TextBox1_Change() TextBox1
MaxLength = 6 End Sub 范例 6 9 验证文本框输入的数据 Private Sub CommandButton1_Click() With TextBox1 If (Len(Trim(
Text))) = 15 Or (Len(Trim(
Text)))