How to Create a Read-Only Text Box Using SendMessage API
ID: Q110403
|
The information in this article applies to:
-
Microsoft Visual Basic Standard and Professional Editions for Windows, versions 2.0, 3.0
SUMMARY
Visual Basic does not have a Read-Only property for a text box. But you can
create a read-only text box by calling the Windows API SendMessage function
with the EM_SETREADONLY message.
MORE INFORMATION
Setting the text box state to read-only allows the user to scroll and
highlight the text in the text box, but does not allow them to edit it.
The program can still modify the text by changing the text property.
To create a read-only text box, call the Windows API SendMessage function,
using the EM_SETREADONLY message constant as the second parameter. The
SendMessage function requires the following parameters:
ret& = SendMessage(hWnd%, uMsg%, wParam%, lParam&)
where:
ret& holds the return value of the function call.
hWnd% identifies the window handle that is to receive the message.
uMsg% the message to be sent (EM_SETREADONLY).
wParam% specifies whether to set or remove the read-only state of
the edit control. A value of TRUE sets the state to
read-only; a value of FALSE sets the state to read/write.
lParam& not used for this message, set its value to 0&.
The return value of this function is nonzero if the function was
successful, and it is zero if an error occurred.
Step-by-Step Example
The following example loads a file into a text box, and then sets the
text box state to read-only:
- Start a new project in Visual Basic. Form1 is created by default.
- Add a text box (Text1) to Form1. Select the text box and press the F4
key to display the Properties window. Set the MultiLine property of
Text1 to True and set the Scrollbars property to 3 - Both.
- Add the following constants and declarations to the module level or to
the general declarations of your code:
Const WM_USER = &H400
Const EM_SETREADONLY = (WM_USER + 31)
' Enter the following Declare statement as one, single line:
Declare Function SendMessage Lib "User" (ByVal hWnd As Integer,
ByVal wMsg As Integer, ByVal wParam As Integer,
ByVal lParam As Any) As Long NOTE: If you add the Const declarations to the .BAS module, you need to
delcare them as Global Const.
- Add the following code to the Form_Load Sub procedure:
Sub Form_Load()
Dim TmpStr As String
Dim ret as Long
Open "C:\AUTOEXEC.BAT" For Input As #1
While Not Eof(1)
' Read a line of text in from the input file:
Line Input #1, TmpStr
' Append it to the text box, adding carriage return and line feed:
Text1.Text = Text1.Text & TmpStr & Chr$(13) & Chr$(10)
Wend
' Set the text box to read-only mode:
ret = SendMessage(Text1.hWnd, EM_SETREADONLY, True, 0&)
If ret = 0 Then ' Check the return value for error
MsgBox "Could Not Set Text Box to Read-Only."
End If
End Sub
- Start the program, or press the F5 key. When the program loads, it will
read the AUTOEXEC.BAT file into the text box, and then set the read-only
state of the text box. Then the user can scroll and highlight the text
in the text box but won't be able to edit it.
Additional query words:
2.00 3.00
Keywords :
Version :
Platform :
Issue type :
|