Results 1 to 18 of 18

Thread: Turn OFF Enter Key on CommandButton [RESOLVED]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2003
    Location
    Marysville, WA
    Posts
    112

    Resolved 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

  2. #2
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Turn OFF Enter Key on CommandButton

    Quote 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.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2003
    Location
    Marysville, WA
    Posts
    112

    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.
    Jim B

  4. #4
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Turn OFF Enter Key on CommandButton

    you could try:
    VB Code:
    1. Private Sub command1_keypress(Keyascii as integer)
    2. if keyascii = 30 then ' i think its 30
    3. then keyascii = 0
    4. end sub

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Turn OFF Enter Key on CommandButton

    Quote Originally Posted by |2eM!x
    you could try:
    VB Code:
    1. Private Sub command1_keypress(Keyascii as integer)
    2. if keyascii = 30 then ' i think its 30
    3. then keyascii = 0
    4. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Apr 2003
    Location
    Marysville, WA
    Posts
    112

    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
    Jim B

  7. #7
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    In Front of my computer...
    Posts
    367

    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!

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  9. #9
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    Re: Turn OFF Enter Key on CommandButton

    prepare to slap foreheads..
    VB Code:
    1. Private Sub Command1_Click()
    2.     'do nothing
    3. End Sub
    4.  
    5. Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    6.     'code here
    7. End Sub

  10. #10
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Turn OFF Enter Key on CommandButton

    :: slap ::

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Turn OFF Enter Key on CommandButton

    Quote Originally Posted by dis1411
    prepare to slap foreheads..
    VB Code:
    1. Private Sub Command1_Click()
    2.     'do nothing
    3. End Sub
    4.  
    5. Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    6.     'code here
    7. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  12. #12
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Turn OFF Enter Key on CommandButton

    Sounds good to me.

  13. #13
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    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:
    1. Dim MouseClicked As Boolean
    2.  
    3. Private Sub Command1_Click()
    4.  
    5.     If Not MouseClicked Then    ' Assume the user pressed enter
    6.         MsgBox "Enter"
    7.         Exit Sub
    8.     Else
    9.         '
    10.         '   Do your button-click stuff here
    11.         '
    12.         MsgBox "Click"
    13.         MouseClicked = False
    14.     End If
    15.    
    16. End Sub
    17.  
    18. Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    19.     '
    20.     '   Flag that the mouse button has been pressed
    21.     '   (May want to check for left or right button etc)
    22.     '
    23.     MouseClicked = True
    24.  
    25. End Sub
    26.  
    27. Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    28.     '
    29.     '   Flag that the mouse button has been released
    30.     '   (May want to check for left or right button etc)
    31.     '
    32.     If MouseClicked = True Then
    33.         MouseClicked = False
    34.     End If
    35.    
    36. End Sub
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

  14. #14
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Turn OFF Enter Key on CommandButton

    even better. the other half slap

  15. #15
    Frenzied Member dis1411's Avatar
    Join Date
    Mar 2001
    Posts
    1,048

    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

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Apr 2003
    Location
    Marysville, WA
    Posts
    112

    Re: Turn OFF Enter Key on CommandButton

    Yet again, thanks for all the help.
    I will accept the forehead slap, Duhhhhhhhhhhhhhhhhhhh.
    Jim B
    Jim B

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  18. #18
    Hyperactive Member
    Join Date
    Nov 2003
    Location
    In Front of my computer...
    Posts
    367

    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
  •  



Click Here to Expand Forum to Full Width