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
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/.
It worked.
Thank you so much for your help.
Your answer did not work.
On CTRL+V, it is pasting Characters also. I need only numbers not characters.
Please help me.
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
@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
}
}
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.
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:
- I will copy "ABCD" .
- Paste in the same textbox which should not allow characters.
- 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.
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;
}
}
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
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).