Capturing TAB from RadTextBox

6 posts, 1 answers
  1. Tim
    Tim avatar
    23 posts
    Member since:
    Nov 2010

    Posted 23 Jan 2011 Link to this post

    Is there a way to capture or trap when the user hits the TAB key while on the RadTextBox?  I've tried various KeyPress, KeyDown, and PreviewKeyDown events but none of them seem to capture the TAB key.  I've tried turning TabStop on and off on the control and turning the KeyPreview property on and off on the ShapedForm but that doesn't appear to changing anything related to this problem.

    Basically I'm trying to capture the TAB key for use with a barcode scanning gun that will scan the barcode then automatically enter a TAB key after the scan.  Unfortunately due to other applications that are using the barcode scanner, I'm forced to deal with the TAB key.  I tried using the Leave event but that event wants to fire all the time including when the control has focus and the user clicks a button or when the control has focus and the form is being closed.  I've been unable to come up with a viable solution just yet.  Any  help would be much appreciated!

    Thanks,
    -Tim
  2. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 24 Jan 2011 Link to this post

    Hi Tim,

    In the case of the tab key, the windows handlers grab the tab key and move the cursor to the next control.  The KeyPress event does not fire for tab when tabbing from one textbox to the next (this is the same for Windows controls too).

    In order to get over this, you can override the ProcessDialogKey. Please consider the following:

    '// To know when we want to capture the tab
    Private m_IsFocused As Boolean

    AddHandler Me.RadTextBox1.Enter, AddressOf RadTextBox_Enter
    AddHandler Me.RadTextBox1.Leave, AddressOf RadTextBox_Leave

    Private Sub RadTextBox_Enter(ByVal sender As Object, ByVal e As EventArgs)
        m_IsFocused = True
    End Sub
    Private Sub RadTextBox_Leave(ByVal sender As Object, ByVal e As EventArgs)
        m_IsFocused = False
    End Sub

    Protected Overrides Function ProcessDialogKey(ByVal keyData As Keys) As Boolean
        If keyData = Keys.Tab AndAlso m_IsFocused Then
            MessageBox.Show("Tab Pressed")
        End If
        MyBase.ProcessDialogKey(keyData)
        Return True
    End Function

    Hope that helps but let me know if you need more information
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Tim
    Tim avatar
    23 posts
    Member since:
    Nov 2010

    Posted 24 Jan 2011 Link to this post

    Hi Richard,

    Thank you for your help.  Your suggestion works like a charm.  I did have to change one line though.  In the ProcessDialogKey function I had to Return False instead of Return True as you suggested.  If I was returning True then I couldn't type anything into the text box.  I didn't look up the documentation but I assume the return value is probably a "Handled" property in which case setting it to False allowed the data to be entered in the control normally.  Again, just speculating but what matters most is that it works!

    Thanks again!!
    -Tim
  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 24 Jan 2011 Link to this post

    Hi Tim,

    Glad that I could help and that this works for you.
    All the best
    Richard
  6. Eric
    Eric avatar
    24 posts
    Member since:
    Jul 2011

    Posted 23 Apr 2012 Link to this post

    Oddly enough, this did not work for me.  I get the same behavior as the KeyDown event.  The event gets all keys but the one I want it seems (TAB).  This is a RadTextBox in a RadWizard if that makes any difference.
  7. Boryana
    Admin
    Boryana avatar
    330 posts

    Posted 24 Apr 2012 Link to this post

    Hi,

    Thank you for writing.

    The behavior you experience is a known issue and has been logged to our Public Issue Tracker under ID 9505. It was resolved in Q1 2012 SP1 (version 2012.1.321).

    I would like to point out that you should not set the TabStop property of RadTextBox to true. If you look carefully at the architecture of the control, you will see that it contains a HostedTextBoxItem that actually holds the focus.

    I hope my answer helps. Let me know if you have further questions.

    Regards,
    Boryana
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready