'e.Handled = false' within 'RadTextBox_KeyPress' event is preventing all the clipboard shortcut keys

2 Answers 395 Views
TextBox
Sathish
Top achievements
Rank 1
Iron
Iron
Iron
Sathish asked on 13 Jan 2023, 02:03 PM | edited on 14 Jan 2023, 09:42 PM
      private void radTextBox1_KeyPress(object sender, KeyPressEventArgs e)
      {
            if (Char.IsLetterOrDigit(e.KeyChar) || (e.KeyChar == (char)(Keys.Back)) ||  e.KeyChar == (char)(Keys.Space))
                e.Handled = false;
            else
                e.Handled = true;
      }
e.Handled = false;

I need e.Handled to false in KeyPress event, and also I want clipboard shortcut keys to be worked perfectly.

i.e: CTRL + A | CTRL + X | CTRL + C | CTRL + V.

Please help me to resolve this issue.

2 Answers, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 16 Jan 2023, 09:07 AM

Hi, Sathish,

Since RadTextBox internally hosts the standard MS TextBox, it is necessary to handle the TextBoxElement.TextBoxItem.TextBoxControl.KeyDown event and set the SuppressKeyPress argument to true if you want to restrict the respective key. Otherwise, leave it with value false:

        public RadForm1()
        {
            InitializeComponent();
             
            this.radTextBox1.TextBoxElement.TextBoxItem.TextBoxControl.KeyDown += TextBoxControl_KeyDown;
        }

        private void TextBoxControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (Char.IsLetterOrDigit((char)e.KeyValue) || ((char)e.KeyValue == (char)(Keys.Back)) || (char)e.KeyValue == (char)(Keys.Space))
            {
                e.SuppressKeyPress = false;
            }
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.V)
            {
                // cancel the "paste" function
                e.SuppressKeyPress = true;
            }
        }

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

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Sathish
Top achievements
Rank 1
Iron
Iron
Iron
commented on 17 Jan 2023, 06:59 AM

It worked.

Thank you so much for your help.

Sathish
Top achievements
Rank 1
Iron
Iron
Iron
commented on 24 Jan 2023, 11:58 AM

Your answer did not work.

On CTRL+V, it is pasting Characters also. I need only numbers not characters.

 

Please help me.

Dess | Tech Support Engineer, Principal
Telerik team
commented on 24 Jan 2023, 02:42 PM

Hi, Sathish,

I would suggest you to consider using RadMaskedEditBox with the appropriate numeric mask since it is appropriate way to handle exactly such cases and restrict the user's input: https://docs.telerik.com/devtools/winforms/controls/editors/maskededitbox/mask-types-and-providers 

0
Sathish
Top achievements
Rank 1
Iron
Iron
Iron
answered on 24 Jan 2023, 12:31 PM

@Dess | Tech Support Engineer, Principal.

Your answer did not work.

On CTRL+V, it is pasting Characters also. I need only numbers in this field not characters.

 

Here is my code:
using System;
using System.Windows.Forms;
using Telerik.WinControls.UI;

namespace ClipboardShortCutKeys
{
    public partial class Lookup : RadForm
    {
        public Lookup()
        {
            InitializeComponent();
        }

        private void ntsTextBoxDMV_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsDigit(e.KeyChar) || (e.KeyChar == (char)(Keys.Back)) || e.KeyChar == (char)(Keys.Space))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        #region Lookup.Designer.cs

        /// <summary>
        /// Required designer variable.
        /// </summary>
        private readonly System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }

            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this._radGroupBoxSearchCriteria = new RadGroupBox();
            this._ntsTextBoxDmv = new RadTextBox();
            this._resultPanel = new RadPanel();
            ((System.ComponentModel.ISupportInitialize)(this._radGroupBoxSearchCriteria)).BeginInit();
            this._radGroupBoxSearchCriteria.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this._ntsTextBoxDmv)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this._resultPanel)).BeginInit();
            this._resultPanel.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            // 
            // radGroupBoxSearchCriteria
            // 
            this._radGroupBoxSearchCriteria.AccessibleRole = System.Windows.Forms.AccessibleRole.Grouping;
            this._radGroupBoxSearchCriteria.Controls.Add(this._ntsTextBoxDmv);
            this._radGroupBoxSearchCriteria.Dock = System.Windows.Forms.DockStyle.Top;
            this._radGroupBoxSearchCriteria.HeaderText = "Search Criteria";
            this._radGroupBoxSearchCriteria.Location = new System.Drawing.Point(0, 0);
            this._radGroupBoxSearchCriteria.Name = "_radGroupBoxSearchCriteria";
            this._radGroupBoxSearchCriteria.Size = new System.Drawing.Size(912, 214);
            this._radGroupBoxSearchCriteria.TabIndex = 0;
            this._radGroupBoxSearchCriteria.Text = "Search Criteria";
            // 
            // ntsTextBoxDMV
            // 
            this._ntsTextBoxDmv.CharacterCasing = System.Windows.Forms.CharacterCasing.Upper;
            this._ntsTextBoxDmv.Location = new System.Drawing.Point(447, 136);
            this._ntsTextBoxDmv.MaxLength = 9;
            this._ntsTextBoxDmv.Name = "_radTextBox1";
            this._ntsTextBoxDmv.Size = new System.Drawing.Size(116, 20);
            this._ntsTextBoxDmv.TabIndex = 13;
            this._ntsTextBoxDmv.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.ntsTextBoxDMV_KeyPress);
            // 
            // ResultPanel
            // 
            this._resultPanel.Dock = System.Windows.Forms.DockStyle.Bottom;
            this._resultPanel.Location = new System.Drawing.Point(0, 296);
            this._resultPanel.Name = "_resultPanel";
            this._resultPanel.Size = new System.Drawing.Size(912, 39);
            this._resultPanel.TabIndex = 1;
            // 
            // VoterLookup
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(912, 576);
            this.Controls.Add(this._radGroupBoxSearchCriteria);
            this.KeyPreview = true;
            this.Margin = new System.Windows.Forms.Padding(1);
            this.MaximizeBox = false;
            this.MinimumSize = new System.Drawing.Size(265, 212);
            this.Name = "ClipBoard Shortcuts";
            // 
            // 
            // 
            this.RootElement.ApplyShapeToControl = true;
            this.RootElement.MinSize = new System.Drawing.Size(265, 212);
            this.Text = "Copy paste form";
            this.Controls.SetChildIndex(this._radGroupBoxSearchCriteria, 0);
            ((System.ComponentModel.ISupportInitialize)(this._radGroupBoxSearchCriteria)).EndInit();
            this._radGroupBoxSearchCriteria.ResumeLayout(false);
            this._radGroupBoxSearchCriteria.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this._ntsTextBoxDmv)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this._resultPanel)).EndInit();
            this._resultPanel.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion
        
        private RadTextBox _ntsTextBoxDmv;
        private RadGroupBox _radGroupBoxSearchCriteria;
        private RadPanel _resultPanel;

        #endregion
    }
}

Dess | Tech Support Engineer, Principal
Telerik team
commented on 24 Jan 2023, 02:45 PM

I have prepared a sample code snippet demonstrating how to restrict the user's input and allow only numerics:

        public RadForm1()
        {
            InitializeComponent();
             
            this.radTextBox1.TextBoxElement.TextBoxItem.TextBoxControl.KeyDown += TextBoxControl_KeyDown;
        }

        private void TextBoxControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (Char.IsDigit((char)e.KeyValue) || ((char)e.KeyValue == (char)(Keys.Back)) || (char)e.KeyValue == (char)(Keys.Space))
            {
                e.SuppressKeyPress = false;
            }
            else
            { 
                e.SuppressKeyPress = true;
            }
        } 
         
    }

I hope this helps.

Sathish
Top achievements
Rank 1
Iron
Iron
Iron
commented on 25 Jan 2023, 11:32 AM

Hi Dess.

Please try to understand my problem here.

------- Point # 1 -------

I used this code.

private void TextBoxControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (Char.IsDigit((char)e.KeyValue) || ((char)e.KeyValue == (char)(Keys.Back)) || (char)e.KeyValue == (char)(Keys.Space))
            {
                e.SuppressKeyPress = false;
            }
            else
            { 
                e.SuppressKeyPress = true;
            }
        } 

Using this code, the problem here is, it is blocking shortcut keys.

i.e., CTRL+A | CTRL+X | CTRL+C | CTRL+V.

 

------- Point # 2 -------

I used you first provided code (You gave it on  16 Jan 2023, 02:37 PM)

private void TextBoxControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (Char.IsLetterOrDigit((char)e.KeyValue) || ((char)e.KeyValue == (char)(Keys.Back)) || (char)e.KeyValue == (char)(Keys.Space))
            {
                e.SuppressKeyPress = false;
            }
            if (e.Modifiers == Keys.Control && e.KeyCode == Keys.V)
            {
                // cancel the "paste" function
                e.SuppressKeyPress = true;
            }
        }

It is pasting characters also.

Example:

  1. I will copy "ABCD" .
  2. Paste in the same textbox which should not allow characters.
  3. In the textbox you will see ABCD. (This should not allow characters).

The reason is below condition is not working.

if (Char.IsDigit((char)e.KeyValue) || ((char)e.KeyValue == (char)(Keys.Back)) || (char)e.KeyValue == (char)(Keys.Space))
            {
                e.SuppressKeyPress = false;
            }

 

I hope you understood in this deep explanation.

If you still don't understand the issue. Please read from the top to bottom.

 

Thanks.

 

 

Dess | Tech Support Engineer, Principal
Telerik team
commented on 27 Jan 2023, 07:48 AM

Hello, Sathish,

I have reviewed the two points that you described. Note that the paste operation can be performed either by pressing Ctrl+V or by using the context menu. The suggested code for using the TextBoxElement.TextBoxItem.TextBoxControl.KeyDown event handles the paste operation by the keyboard. However, the context menu's paste option is not handled this way.

An alternative approach came up in my mind. RadTextBox offers the TextChanging event. The TextChangingEventArgs gives you access to the newly entered string value and according to your requirements you can cancel or not the input. Thus, you wouldn't be bothered to think whether the new input comes from the keyboard or by pasting via the context menu: 
        private void RadTextBox1_TextChanging(object sender, TextChangingEventArgs e)
        {
            //true if it does contain letters
            bool result = e.NewValue.Any(x => char.IsLetter(x));
            if (result)
            {
                e.Cancel = true;
            }
        }
Please give it a try and see how it works for your specific scenario.
Sathish
Top achievements
Rank 1
Iron
Iron
Iron
commented on 27 Jan 2023, 10:31 AM

Hello Dess,

However, you tried to provide me the solution for my issue, so thanks.

But the developer will not be happy for adding so many codes to handle the clipboard shortcuts.

 

Thanks

Dess | Tech Support Engineer, Principal
Telerik team
commented on 30 Jan 2023, 07:26 AM

Hi, Sathish,

You can use only the TextChanging event without handling the previously discussed events. Thus, you will ensure at global level that no letters are entered into the editor no matter how the text is changed (either via typing or by the clipboard).

Tags
TextBox
Asked by
Sathish
Top achievements
Rank 1
Iron
Iron
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Sathish
Top achievements
Rank 1
Iron
Iron
Iron
Share this question
or