How to handle keypress in VB.NET

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sgrec7
    New Member
    • Aug 2007
    • 58

    How to handle keypress in VB.NET

    Hi, sgrec7 here.

    so anyway in VB 6 this is how you would use keyascii's

    private sub form_keypress (keyascii as integer)

    if keyascii = 13 enter key then
    end
    end if

    end sub

    that was easy, but how the f&(# do you do that in VB 8 ???? any help will be much appreciated !! thanks
  • QVeen72
    Recognized Expert Top Contributor
    • Oct 2006
    • 1445

    #2
    Hi,

    IN VB8, it is :

    [code=vb]
    Private Sub TextBox1_KeyUp( ByVal sender As Object, ByVal e As System.Windows. Forms.KeyEventA rgs) Handles TextBox1.KeyUp
    If e.KeyCode = Keys.Enter Then
    MsgBox("Veena")
    End If
    End Sub
    [/code]

    REgards
    Veena

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      A couple of questions.
      • VB6 also had a KeyUp event. Is there an equivalent of KeyPress?
      • What the heck is VB8? Didn't they stop numbering them that way after version 6?

      Comment

      • QVeen72
        Recognized Expert Top Contributor
        • Oct 2006
        • 1445

        #4
        Hi Killer,

        Yes, they stopped Numbering, but still

        VB.Net2003 is VB7
        VB.Net2005 is VB8.

        VB7 and VB8 both have same KeyPress Event..

        REgards
        Veena

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by QVeen72
          Yes, they stopped Numbering, but still
          VB.Net2003 is VB7
          VB.Net2005 is VB8.
          Thanks. This is the first time I've heard them referred to that way.

          But I thought there was also a 2002 version of VB.Net, though. What was that, VB6.5?

          Comment

          • Martin2007
            New Member
            • Jul 2007
            • 21

            #6
            Originally posted by Killer42
            A couple of questions.
            • VB6 also had a KeyUp event. Is there an equivalent of KeyPress?
            • What the heck is VB8? Didn't they stop numbering them that way after version 6?
            Hey, VB8 is another name for VB 2005... there is a good page about it on wikipedia... just wiki VB 2005.

            In response to ur comment, yes there is a Keypress equivalent....

            The following code will capture a keypress and then use the keycodes to determine which key was pressed... in this case check for the enter key...

            Code:
            Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) 
            _ Handles TextBox.KeyPress
                    If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
            'DO SOMETHING'
                    End If
                End Sub
            This particular checks for a keypress in a text box, but i think you can check for anykeypress by replacing the textbox with the name of the form or other component.

            Hope this helps...

            Martin

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Originally posted by Martin2007
              Hey, VB8 is another name for VB 2005... there is a good page about it on wikipedia... just wiki VB 2005.
              Ah yes, I see. QVeen72 was mostly right. VB2003 is referred to as VB7.1.

              Comment

              • sgrec7
                New Member
                • Aug 2007
                • 58

                #8
                Originally posted by Killer42
                A couple of questions.
                • VB6 also had a KeyUp event. Is there an equivalent of KeyPress?
                • What the heck is VB8? Didn't they stop numbering them that way after version 6?
                Yea sorry Killer, it's known as both "VB 8" and "Visual Basic 2005"

                Comment

                • sgrec7
                  New Member
                  • Aug 2007
                  • 58

                  #9
                  Originally posted by Martin2007
                  ...The following code will capture a keypress and then use the keycodes to determine ...
                  You know Martin, I think that might actually work !! thanks!!

                  And i forgot to say before, thanks to the admins for changing the name of my thread more appropriate to the subject. Thanks guys
                  Last edited by Killer42; Sep 2 '07, 10:50 PM. Reason: Shortened excessive quote block

                  Comment

                  • sgrec7
                    New Member
                    • Aug 2007
                    • 58

                    #10
                    Originally posted by Martin2007
                    Hey, VB8 is another name for VB 2005... there is a good page about it on wikipedia... just wiki VB 2005.

                    In response to ur comment, yes there is a Keypress equivalent....

                    The following code will capture a keypress and then use the keycodes to determine which key was pressed... in this case check for the enter key...

                    Code:
                    Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) 
                    _ Handles TextBox.KeyPress
                            If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
                    'DO SOMETHING'
                            End If
                        End Sub
                    This particular checks for a keypress in a text box, but i think you can check for anykeypress by replacing the textbox with the name of the form or other component.

                    Hope this helps...[/i]
                    Martin, that code was sooo correct that I'm going to quote you again !!! there is one problem though...


                    Code:
                    _ Handles TextBox.KeyPress
                    needs to be changed to:


                    Code:
                    Private Sub form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles me.keypress
                    so that it reads it correctly.

                    also say i wanted something to happen on key "2" keyascii value of 50 do i just change:


                    Code:
                     If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
                    to


                    Code:
                    if e.keychar = Microsoft.VisualBasic.chrW(50) then
                    or ......

                    Comment

                    • Martin2007
                      New Member
                      • Jul 2007
                      • 21

                      #11
                      Yep looks like you have the idea... I said in my post that the code handled text box presses, but what you have written will allow for keypresses on whole form.

                      You can use any number in brackets for a keycode, there are lists of what the values are online, I think there is one in MSDN if you just search "keycodes"

                      Glad I could help!

                      Martin

                      Comment

                      Working...