如何防止TextBox中的退格键击?

时间:2013-03-09 21:35:32

标签: c# windows-phone-7 windows-phone keystroke backspace

我想在TextBox中抑制一个键击。要禁止Backspace以外的所有击键,我使用以下内容:

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

但是,我只想在按下的键是Backspace时抑制击键。我使用以下内容:

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

然而,这不起作用。选择开始背后的字符仍然被删除。我在输出中得到“TRUE”,因此正在识别Back键。如何阻止用户按退格键? (我的理由是我想在某些情况下删除单词而不是字符,所以我需要自己处理后退键)。)

7 个答案:

答案 0 :(得分:13)

如果要抑制击键,只需设置e.SuppressKeyPress = true(在KeyDown事件中)。例如,使用以下代码阻止退格键更改文本框中的文本:

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

答案 1 :(得分:3)

在Silverlight中,无法处理系统键事件,例如退格。因此,您可以检测它,但不能手动处理它。

答案 2 :(得分:1)

这要求我们在按键事件之前存储文本框的值。不幸的是,在该事件被触发之前处理退格,所以我们必须在它发生之前捕获它,然后我们可以在关键事件发生后再次更新它。

    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;
    }

答案 3 :(得分:0)

确实没有简单的方法来处理这种情况,但这是可能的。

当我们在KeyDown,TextChanged和KeyUp事件之间跳转时,您需要在类中创建一些成员变量来存储输入文本,光标位置和后退键按下状态。

代码看起来像这样:

    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;
    }

答案 4 :(得分:0)

    string oldText = "";
    private void testTextBlock_TextChanged(object sender, TextChangedEventArgs e)
    {
        if (testTextBlock.Text.Length < oldText.Length)
        {
            testTextBlock.Text = oldText;
            testTextBlock.SelectionStart = oldText.Length;
        }
        else
        {
            oldText = testTextBlock.Text;
        }
    }

答案 5 :(得分:0)

这就是我想要删除之前的( Ctrl Backspace )和下一个字( Ctrl 删除),处理多个后续空白字符(0x09,0x20,0xA0):

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DeleteWord
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            // Tab, space, line feed
            char[] whitespace = {'\x09', '\x20', '\xA0'};
            string text = textBox1.Text;
            int start = textBox1.SelectionStart;

            if ((e.KeyCode == Keys.Back || e.KeyCode == Keys.Delete) && textBox1.SelectionLength > 0)
            {
                e.SuppressKeyPress = true;
                textBox1.Text = text.Substring(0, start) + text.Substring(start + textBox1.SelectionLength);
                textBox1.SelectionStart = start;
                return;
            }

            else if (e.KeyCode == Keys.Back && e.Control)
            {
                e.SuppressKeyPress = true;

                if (start == 0) return;

                int pos = Math.Max(text.LastIndexOfAny(whitespace, start - 1), 0);

                while (pos > 0)
                {
                    if (!whitespace.Contains(text[pos]))
                    {
                        pos++;
                        break;
                    }
                    pos--;
                }

                textBox1.Text = text.Substring(0, pos) + text.Substring(start);
                textBox1.SelectionStart = pos;
            }
            else if (e.KeyCode == Keys.Delete && e.Control)
            {
                e.SuppressKeyPress = true;

                int last = text.Length - 1;

                int pos = text.IndexOfAny(whitespace, start);
                if (pos == -1) pos = last + 1;

                while (pos <= last)
                {
                    if (!whitespace.Contains(text[pos])) break;
                    pos++;
                }

                textBox1.Text = text.Substring(0, start) + text.Substring(pos);
                textBox1.SelectionStart = start;
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Tab)
            {
                textBox1.Paste("\t");
                return true;
            }
            else if (keyData == (Keys.Shift | Keys.Tab))
            {
                textBox1.Paste("\xA0");
                return true;
            }
            return base.ProcessCmdKey(ref msg, keyData);
        }

    }
}

感谢Huy Nguyen e.SuppressKeyPress = true;

如果有选择,删除和退格将删除选择,无论修改键是什么(你不会得到那个丑陋的矩形字符,用于按住 Ctrl

似乎也适用于像这样的字符,虽然它可能没有多大意义(这个字符不是一个整体吗?)

答案 6 :(得分:0)

这是一个对我有用的简单解决方案。

private void MyTxtbox_Keypress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == '\b')
    {
        e.Handled = true;

        return;
    }
}
相关问题