form1.frm

来自「这里有很多很实用的VB编程案例,方便大家学习VB.」· FRM 代码 · 共 55 行

FRM
55
字号
VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "限制文本框的输入"
   ClientHeight    =   2100
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   2100
   ScaleWidth      =   4680
   StartUpPosition =   3  '窗口缺省
   Begin VB.TextBox Text1 
      Height          =   375
      Left            =   720
      TabIndex        =   0
      Top             =   720
      Width           =   3135
   End
   Begin VB.Label Label1 
      Caption         =   "下面的文本框只能输入0123456789/-"
      Height          =   180
      Left            =   600
      TabIndex        =   1
      Top             =   240
      Width           =   3240
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Function ValiText(KeyIn As Integer, ValidateString As String, Editable As Boolean) As Integer
Dim ValidateList As String
Dim KeyOut As Integer
If Editable = True Then
ValidateList = UCase(ValidateString) & Chr(8)
Else
ValidateList = UCase(ValidateString)
End If
If InStr(1, ValidateList, UCase(Chr(KeyIn)), 1) > 0 Then
KeyOut = KeyIn
Else
KeyOut = 0
Beep
End If
ValiText = KeyOut
End Function
 
Private Sub Text1_KeyPress(KeyAscii As Integer)
    '第二个参数为限制字符"0123456789/-"。
    '第三个参数决定能否使用 [Backspace] 键。
    KeyAscii = ValiText(KeyAscii, "0123456789/-", True)
End Sub

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?