这是我想出来的删除前一个单词(CtrlBackspace)和后一个单词(CtrlDelete)的方法,同时处理多个连续的空格字符(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)
{
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)
似乎对于像
这样的字符也有效,尽管这可能没有太多意义(这个字符不是一个完整的单词吗?)