New to Telerik UI for WinFormsStart a free 30-day trial

How to disable backspace and delete keyboard buttons

Updated over 6 months ago

Environment

Product VersionProductAuthor
2022.3.913RadDateTimePicker for WinFormsDinko Krastev

Description

This tutorial demonstrates how to disable the Backspace and Delete keyboard buttons.

Solution

To handle these two buttons, you will need to create a custom MaskDateTimeProvider and override the KeyPress method for the Backspace button and the KeyDown for the Delete button. Here is the sample code.

C#

public Form1()
{
    InitializeComponent();
    this.radDateTimePicker1.DateTimePickerElement.TextBoxElement.Provider = new CustomMaskDateTimeProvider(this.radDateTimePicker1.DateTimePickerElement.TextBoxElement.Mask,this.radDateTimePicker1.Culture, this.radDateTimePicker1.DateTimePickerElement.TextBoxElement);
}
public class CustomMaskDateTimeProvider : MaskDateTimeProvider
{
    public CustomMaskDateTimeProvider(string mask, CultureInfo culture, RadMaskedEditBoxElement owner) : base(mask, culture, owner)
    {
    }

    public override void KeyPress(object sender, KeyPressEventArgs e)
    {
        var key = e.KeyChar;
        if(e.KeyChar == '\b')
        {
            e.Handled = true;
        }
            
        base.KeyPress(sender, e);
    }

    public override void KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode ==  Keys.Delete)
        {
            e.Handled = true;
        }
           
        base.KeyDown(sender, e);
    }
}
In this article
EnvironmentDescriptionSolution
Not finding the help you need?
Contact Support