如何防止文本框中的退格键
本文关键字:何防止 文本 | 更新日期: 2023-09-27 18:03:04
我想在文本框中抑制按键。要抑制除退格键以外的所有按键,我使用以下命令:
private void KeyBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
e.Handled = true;
}
然而,我只想在按下退格键时抑制击键。我使用以下语句:
if (e.Key == System.Windows.Input.Key.Back)
{
e.Handled = true;
}
然而,这不起作用。选择开始后的字符仍然被删除。我确实在输出中得到"TRUE",因此可以识别Back键。如何防止用户按退格键?(我这样做的原因是我想在某些情况下删除单词而不是字符,所以我需要自己处理返回键)
当您想要抑制击键时,只需设置e.SuppressKeyPress = true(在KeyDown事件中)。例如,防止退格键使用以下代码更改文本框中的文本:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Back)
{
e.SuppressKeyPress = true;
}
}
请注意,如果您在txtBox1_KeyUp()
处理程序中尝试此操作,它似乎不会工作(因为KeyDown已经处理了TextBox的事件)。
在Silverlight中,没有办法处理系统键事件,比如退格。因此,您可以检测它,但不能手动处理它。
这要求我们在按下键事件之前存储文本框的值。不幸的是,退格是在事件被触发之前处理的,所以我们必须在事件发生之前捕获它,然后我们可以在key up事件被处理后再次更新它。
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;
}
确实没有简单的方法来处理这种情况,但这是可能的。
您需要在类中创建一些成员变量来存储输入文本的状态、光标位置和在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;
}
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;
}
}
这就是我想要删除前一个单词(CtrlBackspace)和下一个单词(Ctrl delete ),处理多个后续空白字符(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 for e.SuppressKeyPress = true;
!
如果有一个选区,删除键和退格键都会删除选区,不管修改键是什么(你不会因为按住Ctrl而得到难看的矩形字符)
似乎也适用于像
这样的字符,尽管它可能没有多大意义(这个字符不是一个完整的单词吗?)
这对我来说是一个简单的解决方案。
private void MyTxtbox_Keypress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == ''b')
{
e.Handled = true;
return;
}
}
将此文本放入textBox(示例:textBox18)的KeyDown事件中:
private void textBox18_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true; // cancels the typed character
if (e.KeyValue != 8) // checks if key typed is Backspace
{
e.SuppressKeyPress = false; // if it is not Backspace then reactivates/enables the typed character
}
}