이것은을 (Ctrl 키 삭제), 여러 이후의 공백 문자 (은 0x09, 0x20에, 0xA0에) 처리 : e.SuppressKeyPress = true;
에 대한 후이 구엔에
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);
}
}
}
감사합니다!
이, 선택의 모두 삭제하고 백 스페이스
(당신은 Ctrl 키을 보유하기위한 못생긴 사각형 문자를받지 않습니다) 변경 키에 관계없이 선택을 삭제하기로
같은 문자를 위해 작동하는 것 같다 경우 글쎄,별로 의미가 없을 수도 있지만 (이 문자는 전체 단어가 아닌가?)
"PreviewKeyDown"과 같은 이벤트가 없습니까? –
Windows Phone 용 Silverlight는 PreviewKeyDown 구현을 수행하지 않습니다. –