Results 1 to 5 of 5

Thread: keypress event question

  1. #1

    Thread Starter
    Registered User jkw119's Avatar
    Join Date
    Oct 2001
    Location
    Pittsburgh
    Posts
    256

    keypress event question

    I have a question, lets say i have the following

    VB Code:
    1. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
    2.         If e.KeyChar = "=" Then
    3.             MsgBox("You Pressed "="")
    4.         End If
    5. End Sub

    how can i do the same thing with enter? if anyone knows how to do this, i would greatly appreciate it.

    thanks,
    Last edited by jkw119; Sep 26th, 2002 at 08:56 AM.

  2. #2
    Member
    Join Date
    Sep 2002
    Posts
    37
    Hi

    The only way I could get this working is using the Keydown event.

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown

    Select Case e.KeyCode
    Case Keys.Enter
    MessageBox.Show("You pressed Enter")
    End Select

    End Sub

    Harold Hoffman

  3. #3
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Code:
            If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
                MsgBox("pressed enter!")
            End If
    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemWindowsFormsKeyPressEventArgsClassKeyCharTopic.htm

    Somewhere buried in the help is also a nice table of which ASCII/Unicode numbers correspond to keys/characters also.

    This also works:
    Code:
            If e.KeyChar = Microsoft.VisualBasic.ChrW(Keys.Enter) Then
                MsgBox("pressed enter!")
            End If

    ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfSystemWindowsFormsKeysClassTopic.htm

    ps: duh, both on the KeyPress event, not KeyDown
    Last edited by Slow_Learner; Sep 26th, 2002 at 11:11 AM.

  4. #4
    Lively Member
    Join Date
    Aug 2001
    Posts
    65
    Are you wanting to move to another control when the enter key is pressed on the keypressed event?, is that what you are asking?

    If so, this below works.

    Code:
    Private Sub txtDataFormat_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDataFormat.KeyPress
    
            'Ceck if enter was pressed
            If e.KeyChar = Microsoft.VisualBasic.ChrW(13) Then
                e.Handled = True
    
                'Is this textbox active or not?
                If (Me.txtDisplay.Enabled = True) Then
                    'Ok go here then
                    Me.txtDisplay.Focus()
                Else
                    'No it's not so move to the command button instead
                    Me.cmdClose.Focus()
                End If
            End If
    
        End Sub
    Hope that helps.

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083

    Lightbulb another way

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    Dim ky As Integer
    ky = Asc(e.KeyChar)
    Select Case ky
    Case Asc(ControlChars.Cr)
    TextBox2.Focus()
    e.Handled = False
    End Select

    End Sub

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