Ok, I'm just messing around with subclassing. How would I subclass a text boxes keydown event so if a user presses a key, the key is canceled so nothing gets entered into the textbox. thanks
Printable View
Ok, I'm just messing around with subclassing. How would I subclass a text boxes keydown event so if a user presses a key, the key is canceled so nothing gets entered into the textbox. thanks
Hi Paul,
There is already an intrinsic event on that in VB. Textbox_keydown event.
But if you want the Windows Message, I will have to get back to you on that
Yeah, I know there is an intrinsic keypress event for the textbox, but I'm just Messing around with subclassing. Oh by the way, my name isn't Paul :D My Name is Marco
That's correct, you don't event need subclasing, but if you want it, here it is:
Add to a Module
Add to a FormCode:Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
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
Const GWL_WNDPROC = (-4)
Const WM_CHAR = &H102
Global WndProcOld As Long
Public Function WindProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If wMsg = WM_KEYDOWN Then Exit Function
WindProc = CallWindowProc(WndProcOld&, hwnd&, wMsg&, wParam&, lParam&)
End Function
Sub SubClassWnd(hwnd As Long)
WndProcOld& = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindProc)
End Sub
Sub UnSubclassWnd(hwnd As Long)
SetWindowLong hwnd, GWL_WNDPROC, WndProcOld&
WndProcOld& = 0
End Sub
Code:Private Sub Form_Load()
SubClassWnd Text1.hwnd
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnSubclassWnd Text1.hwnd
End Sub
Ok Paul,
The windows message you want is the constant WM_KeyDown = &H100
When you reveive this message from the default windows, exit the function (therefore it does nothing). Any other messages, you HAVE to pass along to the new window.
Remember to unhook the function once you have finished or your VB will crash !!!
Thanks Megatron, I know there was an event already...
I just wanted to see an example used with subclassing,
just to get my feet wet so to speak...:D
Hi Guys,
Sorry Marco....:)
Hi Megatron, I am still working in the scrollable forms you have helped me with. Dont you ever sleep...:)
No problem about the Paul thing....My name always get pronouned backwards :rolleyes:
Megatron, I tried your code and vb crashed so I added the WM_KeyDown constant and when I entered something into the text box, nothing happened. Am I missing something. thanks, I'm new to subclassing
Nothing happened, because you have already subclassed that message...meaning your textbox will not receive anything once it receives the Keydown event.
No No...I mean that text was still able to be entered, the whole point of the subclassing the textbox is so that
nothing will be entered.
Anyone?? I just hate putting topics to rest before I know what is going on.
I am still trying to find the correct message.
Megatronic's Codes works fine. But I think Windows recieves a message before the WM_keyDown event and I am still trying to figure out what it is...
Use the X in the corner instead of the Stop button in the VB IDE. If you don't, then it will crash.
SoftwareMaker: There is no message before WM_KEYDOWN. This is the first message, but the TextBox ignores it, and processes the WM_CHAR message instead.
My code works fine, but whenever you subclass in VB, you need to make sure tht you close it with the X in the corner (rather than the stop button), otherwise it will crash.
Ok, I figured it out. Megatron, you used KeyDown instead of WM_Char. Thank You once again
Sorry Guys,
I tried out Megatronics Codes as it is...and Textbox still receives the messages.
Why is that so ? Sorry for the stupid question
Change the code in the form module to this:
Code:Public Function WindProc(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If wMsg = WM_CHAR Then
End If
WindProc = CallWindowProc(WndProcOld&, hwnd&, wMsg&, wParam&, lParam&)
End Function
OK, I got it....Oops.. Syntax Error....:)
Hi Megatron,
Did you figure out the scrollable forms you helped me with.
The form scrolls perfectly and from right to left when I move the scrollBar from left to right BUT when I scroll the bar the other way around, the form STILL scrolls from left to right and all the controls disappears into the left.
Hi M,
I have tweaked it so that now the controls can move accordingly to the directions I scrolled.
All I did was to check for descending or ascending Hscroll values and then if it was descending value, move the controls to the left...If it was ascending, move it to the right.
Thanks for all your help. I really appreciate it very much.
Hey Software, just out of curiosity, how did you find out my name. Doesn't matter to me, just wondering :D
You gave me your email a couple of weeks back when I ask for it if I ever needed help. I just kinda gather your name (the wrong one...obviously) from your email...
I remembered you said that its OK to ask you questions as it keeps you on your toes.
Did I refresh your memory now ?
heheh...yes you did. I remember now. :D
Thanks for all your help Marco. I appreciate it.
Ack, I just saw my mistake now, I declared WM_CHAR, but I used WM_KEYDOWN in my code.