Do not enter in TextBox

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.