Locking Box Against Input

Question:
I want to disable a text box without having its text dimmed. I know of one way, which is put the controlinto a picture box and disable the picture box. But I am looking for a solution that maybe uses an APIcall or something that does not involve using another control. Thank you.

Answer:
For VB4.0 it’s easy, just set the Locked property to True on the control. That will prevent the user from entering text and also prevent the box from showing the text as disabled.For VB3.0 it’s somewhat harder. You need to use the SendMessage API call. The following code should do it for you, as always, make sure each declare is on one line:

‘the main function declareDeclare Function SendMessage Lib “User” (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As LongConst WM_USER = &H400    ‘base constant for API SendMessage function’Const for SendMessage to set/unset a textbox’s read only statusConst EM_SETREADONLY = (WM_USER + 31)’this is called from elsewhere to set/unset the readonly status of a textbox.  Pass it True to set and False’to unset (passed as “flag”)Sub RoSet (flag As Integer)Dim retVal As LongDim i As IntegerDim msg As String’this just sets up the proper string for the message box in the errortrapSelect Case flag  Case True    msg = “Could Not Set One or More Textboxes To Read-Only Status!”  Case False    msg = “Could Not Remove Read-Only Status From One or More Textboxes!”End Select’please note that this code is from a program I wrote where I wanted to set/unset more than one’textbox at a time.  The important line in the next loop is:’retVal = SendMessage(frmMain.Controls(i).hWnd, EM_SETREADONLY, flag, 0&)’simply change “frmMain.Controls(i).hWnd” to reflect the textbox(es) you wish to changeFor i = 0 To frmMain.Controls.Count – 1    If TypeOf frmMain.Controls(i) Is TextBox Then        retVal = SendMessage(frmMain.Controls(i).hWnd, EM_SETREADONLY, flag, 0&)        GoSub TextBoxError    End IfNext i        Exit SubTextBoxError:If retVal = 0 Then    ‘ Check the return value for error    MsgBox msgEnd IfReturnEnd Sub

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: