CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2003
    Location
    INDia
    Posts
    13

    keypress event for command button is not working for "Enter" key why?

    The code which i write below is not working i didn't get why?

    keypress event for command button is not working for "Enter" key why?

    please reply

    Private Sub Command1_KeyPress(KeyAscii As Integer)
    If KeyAscii = vbKeyReturn Then
    MsgBox "HI"
    End If
    End Sub

  2. #2
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210


    i tried it .. yes it does not work

    to detect enter the Click() event is fired when you press enter

  3. #3
    Join Date
    Jan 2003
    Location
    INDia
    Posts
    13
    i know it work with click() but why not with "key press "

    reply

  4. #4
    Join Date
    Feb 2003
    Location
    Bilbao
    Posts
    513
    Return is not a "normal key", and does not call a "KeyPress" message. Neither it does Ctrl, Bloq Mays...
    Caronte
    Si tiene solución... ¿por qué te preocupas?
    Si no tiene solución... ¿por qué te preocupas?

  5. #5
    Join Date
    Jan 2001
    Posts
    486
    Not sure of the reason(s) why [perhaps the gurus here can tell you], but the following code works in the KeyPress event:

    Private Sub Form_KeyPress(KeyAscii As Integer)
    If KeyAscii = 13 Then
    Label1 = "13"
    Label3.Caption = "Enter key."
    End If
    End Sub
    If kids were left to their own devices, would they ever come up with a thing like war?......The Wheel / Todd Rundgren

    Do canibals not eat clowns because they taste funny?

  6. #6
    Join Date
    Jun 2001
    Location
    Mi
    Posts
    1,249
    Doof,

    What you're testing against is the scancode of the key pressed whereas Sa is testing against a VB character.

  7. #7
    Join Date
    Jan 2001
    Posts
    486
    MOwen.... of what you say I am well aware.

    Happened to notice that one works and one does not.

    I just don't know the reason(s) why.
    If kids were left to their own devices, would they ever come up with a thing like war?......The Wheel / Todd Rundgren

    Do canibals not eat clowns because they taste funny?

  8. #8
    Join Date
    Jan 2001
    Posts
    486
    Oooops ..............my appologies. Just noticed I was testing the wrong thing. Please excuse my error.
    If kids were left to their own devices, would they ever come up with a thing like war?......The Wheel / Todd Rundgren

    Do canibals not eat clowns because they taste funny?

  9. #9
    Join Date
    Sep 2001
    Posts
    49
    I have the same problem a long time ago but I found out that if you use KeyUp or KeyDown, it will work properly. That what I use it most applications I develop here.

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

Featured