by Joe Anzalone (3 Submissions)
Category: Custom Controls/Forms/Menus
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Tue 5th March 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Make a Combo Box Not Editable, and disable the right click menu so users can't paste or press the delete key.
API Declarations
'****Put this in the Module as well
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public OldWindowProc As Long
'-- Public Consts
Public Const GWL_WNDPROC = (-4)
Public Const WM_USER = &H400
'--Find the ComboBox's Edit control.
m_EditHWnd = FindEditChild(combo1.hwnd)
'--Set the control's new WindowProc.
OldWindowProc = SetWindowLong(m_EditHWnd, GWL_WNDPROC, AddressOf NoPopupWindowProc)
End Sub
Private Sub Form_Unload(Cancel As Integer)
'-- Lets set the Window proc back to its origonal state
SetWindowLong m_EditHWnd, GWL_WNDPROC, OldWindowProc
End Sub
Private Sub combo1_KeyDown(KeyCode As Integer, Shift As Integer)
'-- On a Key down check to see if the button one or button two was
'-- Pressed on the mouse. I use KeyCode because the combobox dose
'-- Not have mouse controls. We do not want to Lock the combo with
'-- Mouse selections, only when a user selects a key on the keyboard!
If KeyCode = 40 Or KeyCode = 38 Then
Else
combo1.Locked = True
End If
End Sub
Private Sub combo1_KeyUp(KeyCode As Integer, Shift As Integer)
'-- Lets Unlock the combo box when the user lets go of the key
combo1.Locked = False
End Sub
'**********************************
'--Place this in a Module
'**********************************
'--Return this control's class name.
Public Function ClassName(ByVal hwnd As Long) As String
Dim buf As String
Dim buflen As Long
buflen = 256
buf = Space$(buflen - 1)
buflen = GetClassName(hwnd, buf, buflen)
ClassName = Left$(buf, buflen)
End Function
'--Return the hWnd of this control's Edit control child.
Public Function FindEditChild(ByVal hwnd As Long) As Long
Const GW_CHILD = 5
Const GW_HWNDNEXT = 2
Dim child_hwnd As Long
child_hwnd = GetWindow(hwnd, GW_CHILD)
Do While child_hwnd <> 0
If ClassName(child_hwnd) = "Edit" Then
FindEditChild = child_hwnd
Exit Function
End If
child_hwnd = GetWindow(child_hwnd, GW_HWNDNEXT)
Loop
FindEditChild = 0
End Function
'--Pass along all messages except the one that makes the context menu appear.
Public Function NoPopupWindowProc(ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const WM_CONTEXTMENU = &H7B
If msg <> WM_CONTEXTMENU Then _
NoPopupWindowProc = CallWindowProc( _
OldWindowProc, hwnd, msg, wParam, _
lParam)
End Function
No comments have been posted about Make a Combo Box Not Editable, and disable the right click menu so users can't paste or press the d. Why not be the first to post a comment about Make a Combo Box Not Editable, and disable the right click menu so users can't paste or press the d.