This is a migrated thread and some comments may be shown as answers.

Capturing TAB from RadTextBox

9 Answers 138 Views
TextBox
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 1
Tim asked on 24 Jan 2011, 01:24 AM
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

9 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 24 Jan 2011, 10:36 AM
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
0
Tim
Top achievements
Rank 1
answered on 24 Jan 2011, 10:54 AM
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
0
Richard Slade
Top achievements
Rank 2
answered on 24 Jan 2011, 10:56 AM
Hi Tim,

Glad that I could help and that this works for you.
All the best
Richard
0
Eric
Top achievements
Rank 1
answered on 24 Apr 2012, 01:22 AM
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.
0
Boryana
Telerik team
answered on 24 Apr 2012, 02:34 PM
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 >>
0
Sidath
Top achievements
Rank 1
answered on 07 Aug 2020, 06:10 AM

Hi,

currently I am trying to capture key stroke combinations within the ProcessDialogKey, like Ctrl+A or Alt+F4.

I couldn't find out anything about this. Is this even possible with this Method?

- Sidath

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 07 Aug 2020, 06:43 AM

Hello, Sidath,

This seems to be more like a general programming question. After further research in the appropriate forums like StackOverflow and MSDN, I have found the following useful threads:

https://stackoverflow.com/questions/8459067/winforms-capturing-the-combination-of-key-presses-using-processcmdkey-vs-keydo

https://social.msdn.microsoft.com/Forums/en-US/a48afcd0-d885-41aa-973f-884c78d635e3/what-is-the-technically-correct-way-to-handle-ctrlanykey-in-processcmdkey?forum=csharpgeneral

Overriding the Form's ProcessCmdKey method allows you to detect key combinations, e.g. Ctrl+A: 

    public partial class RadForm1 : Telerik.WinControls.UI.RadForm
    {
        public RadForm1()
        {
            InitializeComponent();

        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.A | Keys.Control) )
            {
                Console.WriteLine("Ctrl+A");
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }
    }

I hope this information helps. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

0
Sidath
Top achievements
Rank 1
answered on 07 Aug 2020, 07:03 AM

Sorry I should've mention what I am trying to accomplish.

As the Telerik DateTimePicker doesn't fit my needs I built my own which inherits from RadTextBox.

I override ProcessDialogKey() to avoid dealing with anything else than numbers, dot, modifiers, esc and tab.

So with this method I only have one key registered when pressed if I'm not wrong.

Therefore I didn't come to any result with this.

With the method you provided just nothing happens.

BTW I am using Progress 12.2 for coding.

 

- Sidath

0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 10 Aug 2020, 09:00 AM

Hello, Sidath,

According to the provided information, it is not clear why RadDateTimePicker doesn't fit your needs. Could you please give us some more details about the exact goal that you are trying to achieve and how RadDateTimePicker affects this requirement? Once, we get better understanding of the precise case, it would be able to think about a suitable solution and provide further assistance. Thank you in advance for your cooperation.

I have created a derivative of RadTextBox on my end and override both, the ProcessDialogKey and ProcessCmdKey methods. Both of them seems to detect pressing Ctrl+A

    public class CustomTextBox : RadTextBox
    {
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == (Keys.A | Keys.Control))
            {
                Console.WriteLine("ProcessCmdKey Ctrl+A");
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == (Keys.A | Keys.Control))
            {
                Console.WriteLine("ProcessDialogKey Ctrl+A");
            }
            return base.ProcessDialogKey(keyData);
        }
    }

Is there anything specific in the setup that you have on your end? Do I need to perform any other changes?

Off topic, please have in mind that we offer RadPopupEditor which may be suitable for your scenario. You can host any control, e.g. RadCalendar, if it is necessary, in the popup. Additional information can be found here: https://docs.telerik.com/devtools/winforms/controls/editors/popupeditor/popupeditor

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Tags
TextBox
Asked by
Tim
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Tim
Top achievements
Rank 1
Eric
Top achievements
Rank 1
Boryana
Telerik team
Sidath
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or