C# 如何检查剪贴板与按键已执行的检查相同?
我没有找到一个事件,我可以验证我粘贴的每个字符。我需要用ASCII码验证“因为我想处理”和“ 按键事件:C# 如何检查剪贴板与按键已执行的检查相同?,c#,clipboard,keypress,C#,Clipboard,Keypress,我没有找到一个事件,我可以验证我粘贴的每个字符。我需要用ASCII码验证“因为我想处理”和“ 按键事件: private void txt_KeyPress(object sender, KeyPressEventArgs e) { if( e.KeyChar == 34 || e.KeyChar == 39)//34 = " 39 = ' { e.Handled = true; } } 简单解决方案: private void txt_Text
private void txt_KeyPress(object sender, KeyPressEventArgs e)
{
if( e.KeyChar == 34 || e.KeyChar == 39)//34 = " 39 = '
{
e.Handled = true;
}
}
简单解决方案:
private void txt_TextChanged(object sender, EventArgs e)
{
string text = txt.Text;
while (text.Contains("\"") || text.Contains("'")) text = text.Replace("\"", "").Replace("'", "");
txt.Text = text;
}
您可以使用
clipboard.GetText()
访问剪贴板文本,还可以通过覆盖控件的WndProc并查看消息0x302(WM_粘贴)来截获低级Windows消息
输入控件是什么?很可能您需要:
textBox.TextChanged+=eventhandler;
@hanspassant这并不能解决我的问题,因为我正在尝试处理这个字符,所以,我不能使用字符串或字符来替换,我需要使用ascii代码。我将使用什么事件?你为什么认为你从剪贴板粘贴了一个字符?你粘贴了一个字符如果我粘贴这个示例:“hello world”…我想替换:hello world而不是“我尝试过使用,但它不起作用:string text=txt.text;If(text.Contains(Convert.ToChar(34)))text.replace(Convert.ToChar(34),”;If(text.Contains(Convert.ToChar(39)))text.replace(Convert.ToChar(39),“”);哦,你想从剪贴板文本中删除单引号和双引号。请查看我编辑过的答案。我不知道该怎么办..私有void TEXTBOX1_TextChanged(object sender,EventArgs e){WndProc(?)}受保护的覆盖void WndProc(ref Message m){if(m.Msg==0x302&&Clipboard.ContainsText()){var cbText=Clipboard.GetText(TextDataFormat.Text);cbText=cbText.Replace(“,”).Replace(“\”,”);//this.SelectedText=cbText;您必须创建一个从TextBox
控件继承的自定义控件。我将用一个更完整的示例更新我的答案。假设我的答案解决了您的问题,您能接受吗?
namespace ClipboardTests
{
using System.Windows.Forms;
public partial class Form1 : Form
{
private MyCustomTextBox MyTextBox;
public Form1()
{
InitializeComponent();
MyTextBox = new MyCustomTextBox();
this.Controls.Add(MyTextBox);
}
}
public class MyCustomTextBox : TextBox
{
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x302 && Clipboard.ContainsText())
{
var cbText = Clipboard.GetText(TextDataFormat.Text);
// manipulate the text
cbText = cbText.Replace("'", "").Replace("\"", "");
// 'paste' it into your control.
SelectedText = cbText;
}
else
{
base.WndProc(ref m);
}
}
}
}