有個方式可以將 Textbox 限制只能輸入 "數字"
OnlyForTextBoxModule.bas
語法:
Option Explicit
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
(ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Const ES_NUMBER = &H2000
Private Const GWL_STYLE = (-16)
' 只允許輸入數字:
Public Function NumbersOnly(tBox As TextBox)
Dim DefaultStyle As Long
DefaultStyle = GetWindowLong(tBox.hWnd, GWL_STYLE)
NumbersOnly = SetWindowLong(tBox.hWnd, GWL_STYLE, DefaultStyle Or ES_NUMBER)
End Function
如何使用,只要在 Private Sub Form_Load()裡加上
NumbersOnly TextBox1
就好了