dcsimg
Results 1 to 19 of 19

Thread: [RESOLVED] Add KeyDown event for pure API TextBoxW (no Subclassing)

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Resolved [RESOLVED] Add KeyDown event for pure API TextBoxW (no Subclassing)

    I used a simple and robust UniCode-TextBox written by Olaf to handle English phonetic characters. It works very well.

    http://www.vbforums.com/showthread.p...=1#post5126167

    http://www.vbforums.com/showthread.p...=1#post5126791

    http://www.vbforums.com/showthread.p...=1#post5128495


    Now I need to add Ctrl+A to it to select all the text of the UniCode-TextBox. That is, I need to make this UniCode-TextBox trigger the KeyDown event. Is there a way to trigger a KeyDown event without subclassing? Thanks.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,464

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    API windows receive no events from VB. VB may receive notification messages, but it doesn't expose them to you. And that wouldn't help in this case.

    If the API window has focus and you have a menu item with Ctrl+A as a shortcut for that menu item, does pressing Ctrl+A trigger that menu item's click event? I'm guessing not because I think the API textbox traps those key strokes. But if it does, then that could be a non-subclass solution. Otherwise, looks like subclassing might be your best option. Though I'd imagine one could get creative & hope for the best, using some timer and testing keyboard presses (other APIs) while that API window has focus.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    Quote Originally Posted by LaVolpe View Post
    API windows receive no events from VB. VB may receive notification messages, but it doesn't expose them to you. And that wouldn't help in this case.

    If the API window has focus and you have a menu item with Ctrl+A as a shortcut for that menu item, does pressing Ctrl+A trigger that menu item's click event? I'm guessing not because I think the API textbox traps those key strokes. But if it does, then that could be a non-subclass solution. Otherwise, looks like subclassing might be your best option. Though I'd imagine one could get creative & hope for the best, using some timer and testing keyboard presses (other APIs) while that API window has focus.

    Hi LaVolpe, Thanks for your reply.

    Although TextBoxW itself has a right-click menu, it seems that there are no shortcuts associated with these menu items. If timer and keyboard testing process can be used to implement the KeyDown event, it's perfectly acceptable.

    In addition, Olaf's TextBoxW is a UserControl, and the TextBox window created by the pure APIs is a child window of this user control. If UserControl.KeyPreview = True can be used to indirectly implement the TextBoxW KeyDown event, that would be great. But I tested UserControl.KeyPreview = True and it doesn't seem to work.

    Edit:

    I put a standard VB.TextBox in a user control and then set UserControl.KeyPreview = True, then I can successfully simulate the TextBox's KeyDown event in UserControl_KeyDown.
    Last edited by dreammanor; Oct 15th, 2019 at 07:58 AM.

  4. #4
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,898

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    Quote Originally Posted by dreammanor View Post
    But I tested UserControl.KeyPreview = True and it doesn't seem to work.
    But the KeyPreview of the form does work.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    Quote Originally Posted by Eduardo- View Post
    But the KeyPreview of the form does work.
    Yes, you reminded me. Although Form.KeyPreview is not the best solution, it also fulfills my needs. Thank you very much, Eduardo.

  6. #6
    Frenzied Member
    Join Date
    Feb 2017
    Posts
    1,898

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    Add to the UserControl code:

    Code:
    Private WithEvents mForm As Form
    Public Event KeyDown(KeyCode As Integer, Shift As Integer)
    
    Private Sub UserControl_InitProperties()
        On Error Resume Next
        Set mForm = Parent
        mForm.KeyPreview = True
    End Sub
    
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        On Error Resume Next
        Set mForm = Parent
        mForm.KeyPreview = True
    End Sub
    
    Private Sub mForm_KeyDown(KeyCode As Integer, Shift As Integer)
        If mFocused Then RaiseEvent KeyDown(KeyCode, Shift)
    End Sub

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    Quote Originally Posted by Eduardo- View Post
    Add to the UserControl code:

    Code:
    Private WithEvents mForm As Form
    Public Event KeyDown(KeyCode As Integer, Shift As Integer)
    
    Private Sub UserControl_InitProperties()
        On Error Resume Next
        Set mForm = Parent
        mForm.KeyPreview = True
    End Sub
    
    Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
        On Error Resume Next
        Set mForm = Parent
        mForm.KeyPreview = True
    End Sub
    
    Private Sub mForm_KeyDown(KeyCode As Integer, Shift As Integer)
        If mFocused Then RaiseEvent KeyDown(KeyCode, Shift)
    End Sub
    Thank you, Eduardo.

  8. #8
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,388

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    @dreammanor - I just tried the code in the first link you provided in the original post, and Ctrl+A selects all text already without any modifications required?

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    Quote Originally Posted by jpbro View Post
    @dreammanor - I just tried the code in the first link you provided in the original post, and Ctrl+A selects all text already without any modifications required?
    Hi jpbro, are you testing Olaf's TextBoxW or LaVolpe's TextBox?

  10. #10
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,388

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    I tried with Olaf's code linked in your first post here: http://www.vbforums.com/showthread.p...=1#post5126167

    Maybe a difference with the Chinese implementation?

    QUESTION: If you right-click the TextBox when there is some text in it, do you see a "Select All" option in the menu that appears?

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    Quote Originally Posted by jpbro View Post
    I tried with Olaf's code linked in your first post here: http://www.vbforums.com/showthread.p...=1#post5126167

    Maybe a difference with the Chinese implementation?

    QUESTION: If you right-click the TextBox when there is some text in it, do you see a "Select All" option in the menu that appears?
    Hi jpbro, thank you for your test. Sorry, I didn't make it clear what I meant. I want to perform a SelectAll operation by pressing Ctrl+A instead of the right-click menu.

  12. #12
    Frenzied Member
    Join Date
    Aug 2010
    Location
    Canada
    Posts
    1,388

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    You were clear, sorry I wasn't I'm saying Ctrl+A works for me to select all with Olaf's code right out-of-the-box with no modifications. I was just wondering if your TextBox had the "Select All" option even available by right-click menu (because if it is not there then I don't think the Ctrl+A shortcut would be available to you). I'm just curious if your textbox has the same features available as mine (Windows 10 1903 18362.388/English).

  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    Quote Originally Posted by jpbro View Post
    You were clear, sorry I wasn't I'm saying Ctrl+A works for me to select all with Olaf's code right out-of-the-box with no modifications. I was just wondering if your TextBox had the "Select All" option even available by right-click menu (because if it is not there then I don't think the Ctrl+A shortcut would be available to you). I'm just curious if your textbox has the same features available as mine (Windows 10 1903 18362.388/English).
    Yes, my UniCode-TextBox also has a right-click menu (and contains the SelectAll menu item), but the shortcut Ctrl+A doesn't seem to work.

    Also, even for the standard VB.TextBox, Ctrl+A doesn't work unless I manually add the code:

    Code:
    Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
        If Shift = 2 And KeyCode = vbKeyA Then
            Text1.SelStart = 0
            Text1.SelLength = Len(Text1.Text)
        End If
    End Sub
    Edit:
    Hi jpbro, sorry, I need to leave the computer for a few hours. After a few hours, I will come back and continue to reply to your message.
    Last edited by dreammanor; Oct 15th, 2019 at 10:41 AM.

  14. #14
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,405

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    Quote Originally Posted by dreammanor View Post
    Also, even for the standard VB.TextBox, Ctrl+A doesn't work ...
    Are you sure, the TextBox in question (the VB-one, or the Unicode-Box) was properly focused,
    before you're trying to do a Ctrl+A on it?

    Olaf

  15. #15
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    270

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    It appears single line TextBoxes automatically recognizes the Ctrl+A shortcut key while multi-line TextBoxes don't; you have to write your own implementation.

  16. #16

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    Quote Originally Posted by Victor Bravo VI View Post
    It appears single line TextBoxes automatically recognizes the Ctrl+A shortcut key while multi-line TextBoxes don't; you have to write your own implementation.
    Hi Victor Bravo VI, on my Chinese computers(Win10, WinXP), Ctrl+A can't automatically implement Select-All operation, regardless of whether the VB.TextBox is single-line or multiple-line.

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    Quote Originally Posted by Schmidt View Post
    Are you sure, the TextBox in question (the VB-one, or the Unicode-Box) was properly focused,
    before you're trying to do a Ctrl+A on it?

    Olaf
    Yes, I'm sure. When I tested the standard VB.TextBox, there was only one control(VB.TextBox) in my test form, and the TextBox was always focused. Regardless of whether the TextBox is single-line or multi-line, Ctrl+A can't implement the Select-All operation unless I write code in Text1_KeyDown to implement the Select-All operation.

  18. #18
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    270

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    Quote Originally Posted by dreammanor View Post
    ... on my Chinese computers(Win10, WinXP), Ctrl+A can't automatically implement Select-All operation, regardless of whether the VB.TextBox is single-line or multiple-line.
    After some experimentation, I think I now know why. Your EXE (or the VB6 IDE) most likely does not have an app manifest that requests v6.0 of comctl32.dll. If your EXE is unmanifested, your Win32 controls won't be themed and single line Edit controls (a.k.a. TextBox) won't respond to Ctrl+A (at least on Windows 7).

    Here's an .RC file that you can compile with RC.EXE to give you a .RES file that you can import to your project. Compile your project and see whether Ctrl+A will now be recognized by single line TextBoxes or not.

    Code:
    // Save this as "AppManifest.rc"
    // Compile it with: "%ProgramFiles%\Microsoft Visual Studio\VB98\Wizards\RC.EXE" "X:\Optional\Absolute\Path\To\AppManifest.rc"
    
    #define MANIFEST_RESOURCE_ID  1
    #define RT_MANIFEST          24
    
    MANIFEST_RESOURCE_ID RT_MANIFEST
    {"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?>\
    \r\n<assembly xmlns=""urn:schemas-microsoft-com:asm.v1"" manifestVersion=""1.0"">\
    \r\n    <dependency>\
    \r\n        <dependentAssembly>\
    \r\n            <assemblyIdentity\
    \r\n                language=""*""\
    \r\n                name=""Microsoft.Windows.Common-Controls""\
    \r\n                processorArchitecture=""x86""\
    \r\n                publicKeyToken=""6595b64144ccf1df""\
    \r\n                type=""win32""\
    \r\n                version=""6.0.0.0"" />\
    \r\n        </dependentAssembly>\
    \r\n    </dependency>\
    \r\n    <trustInfo xmlns=""urn:schemas-microsoft-com:asm.v3"">\
    \r\n        <security>\
    \r\n            <requestedPrivileges>\
    \r\n                <requestedExecutionLevel\
    \r\n                    level=""asInvoker""\
    \r\n                    uiAccess=""false"" />\
    \r\n            </requestedPrivileges>\
    \r\n        </security>\
    \r\n    </trustInfo>\
    \r\n    <compatibility xmlns=""urn:schemas-microsoft-com:compatibility.v1"">\
    \r\n        <application>\
    \r\n            <supportedOS Id=""{E2011457-1546-43C5-A5FE-008DEEE3D3F0}""/><!-- Windows Vista -->\
    \r\n            <supportedOS Id=""{35138B9A-5D96-4FBD-8E2D-A2440225F93A}""/><!-- Windows 7 -->\
    \r\n            <supportedOS Id=""{4A2F28E3-53B9-4441-BA9C-D69D4A4A6E38}""/><!-- Windows 8 -->\
    \r\n            <supportedOS Id=""{1F676C76-80E1-4239-95BB-83D0F6D0DA78}""/><!-- Windows 8.1 -->\
    \r\n            <supportedOS Id=""{8E0F7A12-BFB3-4FE8-B9A5-48FD50A15A9A}""/><!-- Windows 10 -->\
    \r\n        </application>\
    \r\n    </compatibility>\
    \r\n</assembly>\r\n"}

  19. #19

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2012
    Posts
    1,663

    Re: Add KeyDown event for pure API TextBoxW (no Subclassing)

    Hi Victor Bravo VI,

    Yes, you are right. My VB6-IDE does not have an app manifest. But my EXE has it. That is to say, when I compile the source code into an EXE, VB.TextBox and Olaf's Unicode-TextBox automatically have Ctrl+A function on Win10. On XP, Ctrl+A is still invalid, and I need to write code to handle related operations in the Form_KeyDown event.

    Thank you very much for testing and exploring. Also thanks to LaVolpe, Eduardo, jpbro and Olaf.
    Last edited by dreammanor; Oct 17th, 2019 at 06:45 AM.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width