-1

How can I tell if the Tab key has been pressed inside a certain textbox.

I tried enabling the AcceptsTab property to true and creating a function that handles texstboxname.KeyPress event, but when I tab while inside it it just tabs out of the box, instead of hitting my event. (normal keys fire my event, but tab never even fires it)

LarsTech
  • 80,625
  • 14
  • 153
  • 225
PsychoData
  • 1,198
  • 16
  • 32
  • Is your `TextBox` also `Multiline`? `AcceptsTab` *might* only apply to those. Also, try handling `KeyDown` instead of `KeyPress`. – Ry- Mar 05 '14 at 17:41
  • Tabs are used for navigation and intercepted before it reaches the text box. You can derive a class from TextBox and override IsInputKey(). Do strongly consider not confusing the user. – Hans Passant Mar 05 '14 at 18:26
  • This is being used specifically because a tab is generated by the barcode scanners on our floor so they can move around, but this a rather special situation (trying to gather multiple scans in a row) so instead of trying to have our scanners switch from field to field like we normally have it doing, we want it to stay in the same place. – PsychoData Mar 05 '14 at 21:52
  • Plus, I'd rather have the software handle the tab suffix at the end of the data, then make the user switch from a tab suffix to enter suffix or something similar. – PsychoData Mar 05 '14 at 21:54
  • @minitech good point, http://goo.gl/X2XY8j shows us that you should be right. I'll have to test it when I get back to my machine. – PsychoData Mar 05 '14 at 21:56
  • @minitech I tried the KeyDown event, but it still wasn't firing the event for `Tab` unless the multiline property was on. – PsychoData Mar 06 '14 at 14:17

2 Answers2

4

The Multiline property needs to be true as well.

From MSDN (emphasis in bold)

Gets or sets a value indicating whether pressing the TAB key in a multiline text box control types a TAB character in the control instead of moving the focus to the next control in the tab order.

so set the Multiline property to true.

As MiniTech also pointed out, KeyDown is easier to handle since it provides you with an e.KeyCode property whereas the KeyPress event only provides an e.KeyChar property.

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) _
                             Handles TextBox1.KeyDown
  If e.KeyCode = Keys.Tab Then
    'user pressed the Tab key...
  End If
End Sub
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • In my other code in the same program I have ` If e.KeyChar = ChrW(Keys.Return) And txtScanEntryMag.Text <> "" Then ` and it works great. Would this not be the same for the tab? I wont be able to try this until a little later fyi – PsychoData Mar 05 '14 at 21:50
  • Why is KeyDown better than KeyPress? – PsychoData Mar 05 '14 at 21:57
  • @PsychoData: Well, you don’t have to make the textbox multiline to use it, for one. – Ry- Mar 05 '14 at 22:34
  • @PsychoData Keys.Return is your Enter key or Return key depending on your keyboard. Keys.Tab is your Tab key. The Enter key is usually not a navigation key like a Tab key is. So they are very different. – LarsTech Mar 06 '14 at 01:08
  • @larstech On the contrary, enter is the acceptbutton on another part and i was able to prevent it hitting the button for me as the acceptButton. And really? Its rather obvious what `keys.return` is: like hitting the return key. If your answer is essentially "because its not" then just say idk – PsychoData Mar 06 '14 at 05:03
  • @PsychoData I think you are talking about code that I can't see. Somehow my answer seems to have offended you. I don't know why. – LarsTech Mar 06 '14 at 15:48
  • @LarsTech I was frustrated with the comment answer that seems unfounded. I have used several programs where enter moved you to the next field, and in this specific situation enter IS being used to navigate because my form has a defined AcceptButton. I was also frustrated with the fact that you say to use keydown as opposed to KeyPress for no logical reason (if anything I would say keypress is better gives the user a chance to back out after pressing the button but before releasing) since neither of them fires without setting the box to multiline or overriding IsInputKey before before the event – PsychoData Mar 06 '14 at 16:01
  • @PsychoData The enter key does not navigate to the next control by default. There has to be code written by you to do that. The form can map which button is the AcceptButton that gets fired if the user presses the enter key, but that still isn't navigation. See [What's the difference between KeyDown and KeyPress in .NET?](http://stackoverflow.com/q/1367700/719186) in regards to the differences between those events. The tab key isn't a character when the textbox control doesn't have AcceptTab and Multiline equal to true, so hence, `but tab never even fires it`. – LarsTech Mar 06 '14 at 16:17
  • @LarsTech Tab key isnt fired for **EITHER** KeyDown OR KeyPress unless AcceptTab and MultiLine are true – PsychoData Mar 06 '14 at 17:14
  • @PsychoData The Tab key is special to windows navigating the controls on the form. If KeyPress is working for you, then use it. You can always override the form's ProcessCmdKey function to see if the user is pressing the Tab key. – LarsTech Mar 06 '14 at 17:26
  • @LarsTech Thanks, I just handled the `PreviewKeyDown` event and set the tab as an inputkey, then it shows up in `KeyDown`,`KeyPress`, and `KeyUp` – PsychoData Mar 06 '14 at 17:28
2

It seems that you can either Additionally

  1. Enable the multiline property on the TextBox
  2. You can override IsInputKey before the Keydown or Keypress event first.
    • To do that you could derive a class from TextBox and ovverride it there, ( as Hans Passat suggested)
    • or you can also handle the PreviewKeyDown event and ovverride the IsInputKey to true in there.

I went with the second part of option two, so that I wouldn't have to worry about the multiline property being on and could Leave my code with the keypress event

    Private Sub txtEntryBar_PreviewKeyDown(sender As Object, e As PreviewKeyDownEventArgs) Handles txtEntryBar.PreviewKeyDown
        If e.KeyData = Keys.Tab Then
            e.IsInputKey = True
        End If
    End Sub

Ref: http://www.vbforums.com/showthread.php?670904-Detecting-when-the-tab-key-is-pressed-in-a-textbox&p=4124911&viewfull=1#post4124911

Ry-
  • 218,210
  • 55
  • 464
  • 476
PsychoData
  • 1,198
  • 16
  • 32