How to prevent a backspace key stroke in a TextBox?

16,652

Solution 1

It's true that there is no easy way to handle this scenario, but it is possible.

You need to create some member variables in the class to store the state of the input text, cursor position, and back key pressed status as we hop between the KeyDown, TextChanged, and KeyUp events.

The code should look something like this:

    string m_TextBeforeTheChange;
    int m_CursorPosition = 0;
    bool m_BackPressed = false;

    private void KeyBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
    {
        m_TextBeforeTheChange = KeyBox.Text;
        m_BackPressed = (e.Key.Equals(System.Windows.Input.Key.Back)) ? true : false;
    }

    private void KeyBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (m_BackPressed)
        {
            m_CursorPosition = KeyBox.SelectionStart;
            KeyBox.Text = m_TextBeforeTheChange;
        }
    }

    private void KeyBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
    {
        KeyBox.SelectionStart = (m_BackPressed) ? m_CursorPosition + 1 : KeyBox.SelectionStart;
    }

Solution 2

Just set e.SuppressKeyPress = true (in KeyDown event) when you want to suppress a keystroke. Ex, prevent backspace key change your text in textbox using the following code:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Back)
    {
        e.SuppressKeyPress = true;
    }
}

Note that if you try this in the txtBox1_KeyUp() handler, it will not appear to work (because KeyDown already handled the event for the TextBox).

Solution 3

In Silverlight, there is no way to handle system key events, such as backspace. Therefore, you can detect it, but not handle it manually.

Solution 4

This requires that we store the value of the text box before the key down event. Unfortuantely the backspace is handled before that event is fired so we have to capture it before that takes place and then we can update it again after the key up event is procesed.

    private string textBeforeChange;

    private void TextBox1_OnKeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Back)
        {
            e.Handled = true;
            textBox1.Text = textBeforeChange;
        }
    }

    private void TextBox1_OnKeyUp(object sender, KeyEventArgs e)
    {
        textBeforeChange = textBox1.Text;
    }

    private void MainPage_OnLoaded(object sender, RoutedEventArgs e)
    {
        textBox1.AddHandler(TextBox.KeyDownEvent, new KeyEventHandler(TextBox1_OnKeyDown), true);
        textBox1.AddHandler(TextBox.KeyUpEvent, new KeyEventHandler(TextBox1_OnKeyUp), true);
        textBox1.AddHandler(TextBox.ManipulationStartedEvent, new EventHandler<ManipulationStartedEventArgs>(TextBox1_OnManipulationStarted), true);
    }

    private void TextBox1_OnManipulationStarted(object sender, ManipulationStartedEventArgs e)
    {
        textBeforeChange = textBox1.Text;
    }
Share:
16,652
msbg
Author by

msbg

Updated on August 02, 2022

Comments

  • msbg
    msbg over 1 year

    I want to suppress a key stroke in a TextBox. To suppress all keystrokes other than Backspace, I use the following:

        private void KeyBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            e.Handled = true;
        }
    

    However, I only want to suppress keystrokes when the key pressed was Backspace. I use the following:

            if (e.Key == System.Windows.Input.Key.Back)
            {
                e.Handled = true;
            }
    

    However, this does not work. The character behind the selection start is still deleted. I do get "TRUE" in the output, so the Back key is being recognized. How would I prevent the user from pressing backspace? (My reason for this is that I want to delete words instead of characters in some cases, and so I need to handle the back key press myself).)

  • tofo
    tofo over 7 years
    The accepted solution is not the best. Better suggestion below using e.SuppressKeyPress