-
Oct 15th, 2019, 07:00 AM
#1
Thread Starter
PowerPoster
[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.
-
Oct 15th, 2019, 07:07 AM
#2
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.
-
Oct 15th, 2019, 07:46 AM
#3
Thread Starter
PowerPoster
Re: Add KeyDown event for pure API TextBoxW (no Subclassing)
Originally Posted by LaVolpe
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.
-
Oct 15th, 2019, 07:55 AM
#4
Re: Add KeyDown event for pure API TextBoxW (no Subclassing)
Originally Posted by dreammanor
But I tested UserControl.KeyPreview = True and it doesn't seem to work.
But the KeyPreview of the form does work.
-
Oct 15th, 2019, 08:02 AM
#5
Thread Starter
PowerPoster
Re: Add KeyDown event for pure API TextBoxW (no Subclassing)
Originally Posted by Eduardo-
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.
-
Oct 15th, 2019, 08:02 AM
#6
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
-
Oct 15th, 2019, 08:06 AM
#7
Thread Starter
PowerPoster
Re: Add KeyDown event for pure API TextBoxW (no Subclassing)
Originally Posted by Eduardo-
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.
-
Oct 15th, 2019, 08:44 AM
#8
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?
-
Oct 15th, 2019, 09:10 AM
#9
Thread Starter
PowerPoster
Re: Add KeyDown event for pure API TextBoxW (no Subclassing)
Originally Posted by jpbro
@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?
-
Oct 15th, 2019, 09:21 AM
#10
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?
-
Oct 15th, 2019, 09:51 AM
#11
Thread Starter
PowerPoster
Re: Add KeyDown event for pure API TextBoxW (no Subclassing)
Originally Posted by jpbro
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.
-
Oct 15th, 2019, 10:19 AM
#12
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).
-
Oct 15th, 2019, 10:34 AM
#13
Thread Starter
PowerPoster
Re: Add KeyDown event for pure API TextBoxW (no Subclassing)
Originally Posted by jpbro
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.
-
Oct 15th, 2019, 11:08 AM
#14
Re: Add KeyDown event for pure API TextBoxW (no Subclassing)
Originally Posted by dreammanor
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
-
Oct 15th, 2019, 01:31 PM
#15
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.
-
Oct 15th, 2019, 07:29 PM
#16
Thread Starter
PowerPoster
Re: Add KeyDown event for pure API TextBoxW (no Subclassing)
Originally Posted by Victor Bravo VI
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.
-
Oct 15th, 2019, 07:36 PM
#17
Thread Starter
PowerPoster
Re: Add KeyDown event for pure API TextBoxW (no Subclassing)
Originally Posted by Schmidt
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.
-
Oct 16th, 2019, 06:29 AM
#18
Re: Add KeyDown event for pure API TextBoxW (no Subclassing)
Originally Posted by dreammanor
... 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"}
-
Oct 17th, 2019, 06:41 AM
#19
Thread Starter
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|