Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Visual Basic
Expertise: Beginner
Oct 1, 1996

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 control into a picture box and disable the picture box. But I am looking for a solution that maybe uses an API call 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 declare
Declare Function SendMessage Lib "User" (ByVal hWnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, lParam As Any) As Long

Const WM_USER = &H400    'base constant for API SendMessage function

'Const for SendMessage to set/unset a textbox's read only status
Const 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 Long
Dim i As Integer
Dim msg As String

'this just sets up the proper string for the message box in the errortrap
Select 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 change

For 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 If
Next i
        
Exit Sub

TextBoxError:
If retVal = 0 Then    ' Check the return value for error
    MsgBox msg
End If

Return

End Sub
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date