Results 1 to 25 of 25

Thread: Subclassing Question

  1. #1

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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

  2. #2
    Hyperactive Member SoftwareMaker's Avatar
    Join Date
    Mar 2001
    Location
    Elbonia with Dilbert and Wally
    Posts
    322

    Let me check that one out

    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
    William T
    Software Architect / Chief Software Developer
    Softwaremaker.Net Pte Ltd
    http://www.Softwaremaker.net

    *** Things are always the darkest before they go pitch black ***

  3. #3

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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 My Name is Marco

  4. #4
    Guest
    That's correct, you don't event need subclasing, but if you want it, here it is:

    Add to a Module
    Code:
    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
    Add to a Form
    Code:
    Private Sub Form_Load()
        SubClassWnd Text1.hwnd
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        UnSubclassWnd Text1.hwnd
    End Sub

  5. #5
    Hyperactive Member SoftwareMaker's Avatar
    Join Date
    Mar 2001
    Location
    Elbonia with Dilbert and Wally
    Posts
    322

    to answer

    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 !!!
    William T
    Software Architect / Chief Software Developer
    Softwaremaker.Net Pte Ltd
    http://www.Softwaremaker.net

    *** Things are always the darkest before they go pitch black ***

  6. #6

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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...

  7. #7
    Hyperactive Member SoftwareMaker's Avatar
    Join Date
    Mar 2001
    Location
    Elbonia with Dilbert and Wally
    Posts
    322

    Hi guys

    Hi Guys,

    Sorry Marco....

    Hi Megatron, I am still working in the scrollable forms you have helped me with. Dont you ever sleep...
    William T
    Software Architect / Chief Software Developer
    Softwaremaker.Net Pte Ltd
    http://www.Softwaremaker.net

    *** Things are always the darkest before they go pitch black ***

  8. #8

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    No problem about the Paul thing....My name always get pronouned backwards
    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

  9. #9
    Hyperactive Member SoftwareMaker's Avatar
    Join Date
    Mar 2001
    Location
    Elbonia with Dilbert and Wally
    Posts
    322

    Hi Lethal

    Nothing happened, because you have already subclassed that message...meaning your textbox will not receive anything once it receives the Keydown event.
    William T
    Software Architect / Chief Software Developer
    Softwaremaker.Net Pte Ltd
    http://www.Softwaremaker.net

    *** Things are always the darkest before they go pitch black ***

  10. #10

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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.

  11. #11

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Anyone?? I just hate putting topics to rest before I know what is going on.

  12. #12
    Hyperactive Member SoftwareMaker's Avatar
    Join Date
    Mar 2001
    Location
    Elbonia with Dilbert and Wally
    Posts
    322

    Hi Marco

    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...
    William T
    Software Architect / Chief Software Developer
    Softwaremaker.Net Pte Ltd
    http://www.Softwaremaker.net

    *** Things are always the darkest before they go pitch black ***

  13. #13
    Guest
    Use the X in the corner instead of the Stop button in the VB IDE. If you don't, then it will crash.

  14. #14
    Guest
    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.

  15. #15

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Ok, I figured it out. Megatron, you used KeyDown instead of WM_Char. Thank You once again

  16. #16
    Hyperactive Member SoftwareMaker's Avatar
    Join Date
    Mar 2001
    Location
    Elbonia with Dilbert and Wally
    Posts
    322

    Ummmm...

    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
    William T
    Software Architect / Chief Software Developer
    Softwaremaker.Net Pte Ltd
    http://www.Softwaremaker.net

    *** Things are always the darkest before they go pitch black ***

  17. #17

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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

  18. #18
    Hyperactive Member SoftwareMaker's Avatar
    Join Date
    Mar 2001
    Location
    Elbonia with Dilbert and Wally
    Posts
    322

    OK I Got it

    OK, I got it....Oops.. Syntax Error....
    William T
    Software Architect / Chief Software Developer
    Softwaremaker.Net Pte Ltd
    http://www.Softwaremaker.net

    *** Things are always the darkest before they go pitch black ***

  19. #19
    Hyperactive Member SoftwareMaker's Avatar
    Join Date
    Mar 2001
    Location
    Elbonia with Dilbert and Wally
    Posts
    322

    Hi Megatron

    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.
    William T
    Software Architect / Chief Software Developer
    Softwaremaker.Net Pte Ltd
    http://www.Softwaremaker.net

    *** Things are always the darkest before they go pitch black ***

  20. #20
    Hyperactive Member SoftwareMaker's Avatar
    Join Date
    Mar 2001
    Location
    Elbonia with Dilbert and Wally
    Posts
    322

    Hi Megatronics

    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.
    William T
    Software Architect / Chief Software Developer
    Softwaremaker.Net Pte Ltd
    http://www.Softwaremaker.net

    *** Things are always the darkest before they go pitch black ***

  21. #21

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Hey Software, just out of curiosity, how did you find out my name. Doesn't matter to me, just wondering

  22. #22
    Hyperactive Member SoftwareMaker's Avatar
    Join Date
    Mar 2001
    Location
    Elbonia with Dilbert and Wally
    Posts
    322

    Hi Lethal

    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 ?
    William T
    Software Architect / Chief Software Developer
    Softwaremaker.Net Pte Ltd
    http://www.Softwaremaker.net

    *** Things are always the darkest before they go pitch black ***

  23. #23

    Thread Starter
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    heheh...yes you did. I remember now.

  24. #24
    Hyperactive Member SoftwareMaker's Avatar
    Join Date
    Mar 2001
    Location
    Elbonia with Dilbert and Wally
    Posts
    322

    Thanks Marco

    Thanks for all your help Marco. I appreciate it.
    William T
    Software Architect / Chief Software Developer
    Softwaremaker.Net Pte Ltd
    http://www.Softwaremaker.net

    *** Things are always the darkest before they go pitch black ***

  25. #25
    Guest
    Ack, I just saw my mistake now, I declared WM_CHAR, but I used WM_KEYDOWN in my code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width