|
-
Feb 20th, 2005, 12:30 PM
#1
Thread Starter
Lively Member
Turn OFF Enter Key on CommandButton [RESOLVED]
I have users who like to use 'enter' rather 'tab' to register data and move to the next field. Have this working for all controls except the CommandButton. How might I turn off the 'enter' key from executing on a CommandButton???????? It would appear that simply intercepting the enter key and then sendkeys "{TAB}" does not work. Any suggestions??????
Last edited by jbonner; Feb 22nd, 2005 at 09:51 AM.
Jim B
-
Feb 20th, 2005, 12:33 PM
#2
Re: Turn OFF Enter Key on CommandButton
 Originally Posted by jbonner
I have users who like to use 'enter' rather 'tab' to register data and move to the next field. Have this working for all controls except the CommandButton. How might I turn off the 'enter' key from executing on a CommandButton???????? It would appear that simply intercepting the enter key and then sendkeys "{TAB}" does not work. Any suggestions??????
I believe setting the DEFAULT property to FALSE will stop ENTER from taking the command button.
If not, you need to catch the ASCII value 13 in KEYPRESS and change it to 0 - that should suppress it.
-
Feb 20th, 2005, 12:36 PM
#3
Thread Starter
Lively Member
Re: Turn OFF Enter Key on CommandButton
Thanks for the reply.
vbDefault=false on all CommandButton
keyascii = 0 on 'enter' intercept
This does not work.
-
Feb 20th, 2005, 01:20 PM
#4
Re: Turn OFF Enter Key on CommandButton
you could try:
VB Code:
Private Sub command1_keypress(Keyascii as integer)
if keyascii = 30 then ' i think its 30
then keyascii = 0
end sub
-
Feb 20th, 2005, 01:31 PM
#5
Re: Turn OFF Enter Key on CommandButton
 Originally Posted by |2eM!x
you could try:
VB Code:
Private Sub command1_keypress(Keyascii as integer)
if keyascii = 30 then ' i think its 30
then keyascii = 0
end sub
None of these will work. The _Click event fires regardless of what you do. Place a breakpoint
at the start of each of those procedures and you will see that they wont fire from an enter
key being presses, even with Form1.KeyPreview set to True.
Now if the command button does not have the focus and the Default is False
then thats another story.
If you want you can just have the command button never be able to recieve
the focus by setting the TabStop property ot False.
You may have to end up sub-classing the command button and handling the
enter keypress in order to cancel it out if the button in question has the focus.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 20th, 2005, 01:45 PM
#6
Thread Starter
Lively Member
Re: Turn OFF Enter Key on CommandButton
RobDog88:
Neither 'enter' or 'tab' get to to the key events.
I had just verified that when I got your reply.
I not much good at subclassing.
Could you point me to an example.
TIA
-
Feb 22nd, 2005, 12:37 AM
#7
Hyperactive Member
Re: Turn OFF Enter Key on CommandButton
this isnt that simple as i though it would lol
rob said subclass...and even I though subclassing would do it. ok i subclassed cmd button watched for wm_keydown and wparam to be vk_return and nothing. This worked on all other controls exept cmdbuttons :/ why...i dont know...
anyways im thinking of another way to do this...is interesting "challenge"
Born to help others
(If I've been helpful then please rate my post. Thanks)
call me EJ or be slapped! 
-
Feb 22nd, 2005, 12:40 AM
#8
Re: Turn OFF Enter Key on CommandButton
Yes, this one is a little harder then it sounds. Basically you want to disable the click event if invoked
by a enter keypress, but enabled for a mouse click?
I set up a small test project subclassing the cmd button and I tried canceling
a few messages but I havent got the right one yet. I may have more time on
this tomorrow.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 22nd, 2005, 12:58 AM
#9
Frenzied Member
Re: Turn OFF Enter Key on CommandButton
prepare to slap foreheads..
VB Code:
Private Sub Command1_Click()
'do nothing
End Sub
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
'code here
End Sub
-
Feb 22nd, 2005, 01:03 AM
#10
Re: Turn OFF Enter Key on CommandButton
-
Feb 22nd, 2005, 01:11 AM
#11
Re: Turn OFF Enter Key on CommandButton
 Originally Posted by dis1411
prepare to slap foreheads..
VB Code:
Private Sub Command1_Click()
'do nothing
End Sub
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
'code here
End Sub
No slap since the poster wants the command button to function as usual unless its a keyboard enter press.
I know it sounds triavial but there may be consequenses it there are events
taken out of the click event. Order of other events fireing based on the click,
but I say this should work unless we hear otherwise.
Ok, how about a half of a slap? *Sl...
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 22nd, 2005, 01:25 AM
#12
Re: Turn OFF Enter Key on CommandButton
-
Feb 22nd, 2005, 01:39 AM
#13
Re: Turn OFF Enter Key on CommandButton
Here's a slight variation on dis1411's suggestion. A bit long-winded perhaps, but it works:
VB Code:
Dim MouseClicked As Boolean
Private Sub Command1_Click()
If Not MouseClicked Then ' Assume the user pressed enter
MsgBox "Enter"
Exit Sub
Else
'
' Do your button-click stuff here
'
MsgBox "Click"
MouseClicked = False
End If
End Sub
Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
'
' Flag that the mouse button has been pressed
' (May want to check for left or right button etc)
'
MouseClicked = True
End Sub
Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
'
' Flag that the mouse button has been released
' (May want to check for left or right button etc)
'
If MouseClicked = True Then
MouseClicked = False
End If
End Sub
Pete
No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.
-
Feb 22nd, 2005, 01:43 AM
#14
Re: Turn OFF Enter Key on CommandButton
even better. the other half slap
-
Feb 22nd, 2005, 04:59 AM
#15
Frenzied Member
Re: Turn OFF Enter Key on CommandButton
yeah, the part i was debating over weather or not to do 
i figured if i got the ball rolling someone would feel like finishing it
-
Feb 22nd, 2005, 09:48 AM
#16
Thread Starter
Lively Member
Re: Turn OFF Enter Key on CommandButton
Yet again, thanks for all the help.
I will accept the forehead slap, Duhhhhhhhhhhhhhhhhhhh.
Jim B
-
Feb 22nd, 2005, 12:50 PM
#17
Re: Turn OFF Enter Key on CommandButton [RESOLVED]
Nice workaround guys I will not forget this if I ever need something like this in one of my projects. 
Ok, I'll take my full *Slap* now 
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 22nd, 2005, 04:25 PM
#18
Hyperactive Member
Re: Turn OFF Enter Key on CommandButton [RESOLVED]
heh nice
Born to help others
(If I've been helpful then please rate my post. Thanks)
call me EJ or be slapped! 
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
|