Method 1: The cursor blinks, and the input and deletion operations are invalid)
Text1.Locked = True
Method 2: (no blinking cursor, no input or deletion allowed, page color changing, text reversed)
Text1.Enabled = False
Method 3: (the cursor blinks, can be deleted, and cannot be entered)
This method uses two API functions, which is a little more complex. Add two buttons and a text box in the standard project:
Option Explicit
Private Declare Function GetWindowLong Lib "user32"
Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA"
(ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Const GWL_STYLE = (-16)
Const ES_NUMBER = & H2000 &
Public Sub SetNumber (NumberText As TextBox, Flag As Boolean)
Dim CurrentStyle As Long, NewStyle As Long
Return normal style
CurrentStyle = GetWindowLong (NumberText. hwnd, GWL_STYLE)
If Flag Then
CurrentStyle = CurrentStyle Or ES_NUMBER
Else
CurrentStyle = CurrentStyle And (Not ES_NUMBER)
End If
Set New Style
NewStyle = SetWindowLong (NumberText. hwnd, GWL_STYLE, CurrentStyle)
NumberText. Refresh
End Sub
Private Sub commandementclick ()
SetNumber Text1, True
Text1.SetFocus
End Sub
Private Sub Command2_Click ()
SetNumber Text1, False
Text1.SetFocus
End Sub
Private Sub Form_Load ()
Command1.Caption = "forbidden input"
Command2.Caption = "can be entered"
End Sub