Results 1 to 6 of 6

Thread: [RESOLVED] e.keychar = chr(9) not working

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    485

    Resolved [RESOLVED] e.keychar = chr(9) not working

    why does the tab not work?
    Code:
     Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress
            If e.KeyChar = Chr(13) Or e.KeyChar = Chr(9) Then
                'e.KeyChar = Chr(8)
                Form1.words.Add(TextBox2.Text)
                TextBox2.Text = ""
            End If
        End Sub
    Thanks
    George

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: e.keychar = chr(9) not working

    Because the Tab key doesn't trigger the KeyPress event.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    485

    Re: e.keychar = chr(9) not working

    what does the tab key trigger?

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: e.keychar = chr(9) not working

    Quote Originally Posted by georgesutfin View Post
    what does the tab key trigger?
    It triggers moving the focus from one control to the next control in the tab order.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  5. #5
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: e.keychar = chr(9) not working

    Hello

    Use PreviewKeyDown event

    Code:
     ' By default, KeyDown does not fire for the tab key
        Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles TextBox1.KeyDown
            Select Case (e.KeyCode)
                Case Keys.Tab
                    MsgBox("test")
            End Select
        End Sub
    
        ' PreviewKeyDown is where you preview the key.
        ' Do not put any logic here, instead use the
        ' KeyDown event after setting IsInputKey to true.
        Private Sub TextBox1_PreviewKeyDown(ByVal sender As Object, ByVal e As PreviewKeyDownEventArgs) Handles TextBox1.PreviewKeyDown
            Select Case (e.KeyCode)
                Case Keys.Tab
                    e.IsInputKey = True
            End Select
        End Sub
    Last edited by Delaney; Aug 8th, 2020 at 04:49 PM. Reason: add example from docs.microsoft.com
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2013
    Posts
    485

    Re: e.keychar = chr(9) not working

    Thank you so much Delaney that is what I was looking for

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