在c#中处理粘贴事件

我已经创建了一个静态类数字文本框但我不想控制用户在te文本框中粘贴的内容。 对于处理粘贴事件我使用textchanged事件:

static public void textChanged(EventArgs e, TextBox textbox, double tailleMini, double tailleMaxi, string carNonAutorisé) { //Recherche dans la TextBox, la première occurrence de l'expression régulière. Match match = Regex.Match(textbox.Text, carNonAutorisé); /*Si il ya une Mauvaise occurence: * - On efface le contenu collé * - On prévient l'utilisateur */ if (match.Success) { textbox.Text = ""; MessageBox.Show("Votre copie un ou des caractère(s) non autorisé", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Information); } tailleTextBox(textbox, tailleMini, tailleMaxi); } 

在另一个类中,我使用这样的静态方法

  private void tbxSigné_TextChanged(object sender, EventArgs e) { FiltreTbx.textChanged(e, tbxSigné, double.MinValue, double.MaxValue, @"[^\d\,\;\.\-]"); } 

我不想做的是这样的事情:

  if (match.Success) { textbox.Text = //Write the text before users paste in the textbox; } 

有人有想法吗?

首先,你考虑过使用MaskedTextBox吗? 它可以为您处理字符过滤。

但是,为了这个练习,你可以想到沿着这条线的解决方案。 这是用法:

 public Form1() { InitializeComponent(); FiltreTbx.AddTextBoxFilter(tbxSigné, double.MinValue, double.MaxValue, @"[^\d\,\;\.\-]"); } 

这个AddTextBoxFilter是一个新的静态方法,你只调用一次。 它将向TextBox添加TextChanged处理程序。 此处理程序使用闭包将前一个Text存储在文本框中。

您的静态方法获得了一个额外的参数来传递此前一个文本。

 public class FiltreTbx { public static void AddTextBoxFilter(TextBox textbox, double tailleMini, double tailleMaxi, string carNonAutorisé) { string previousText = textbox.Text; textbox.TextChanged += delegate(object sender, EventArgs e) { textChanged(e, textbox, tailleMini, tailleMaxi, carNonAutorisé, previousText); previousText = textbox.Text; }; } static public void textChanged(EventArgs e, TextBox textbox, double tailleMini, double tailleMaxi, string carNonAutorisé, string previousText) { //Recherche dans la TextBox, la première occurrence de l'expression régulière. Match match = Regex.Match(textbox.Text, carNonAutorisé); /*Si il ya une Mauvaise occurence: * - On efface le contenu collé * - On prévient l'utilisateur */ if (match.Success) { // Set the Text back to the value it had after the previous // TextChanged event. textbox.Text = previousText; MessageBox.Show("Votre copie un ou des caractère(s) non autorisé", "Attention", MessageBoxButtons.OK, MessageBoxIcon.Information); } tailleTextBox(textbox, tailleMini, tailleMaxi); } } 

我不确定tailleTextBox究竟应该做什么,你没有包含那个源代码,但我怀疑它强制执行最小值和最大值?

替代解决方案

如果你想自己处理粘贴操作,在它发生之前,你必须拦截WM_PASTE消息到文本框。 一种方法是创建一个专门的控件:

 using System; using System.Windows.Forms; class MyTextBox : TextBox { private const int WM_PASTE = 0x0302; protected override void WndProc(ref Message m) { if (m.Msg != WM_PASTE) { // Handle all other messages normally base.WndProc(ref m); } else { // Some simplified example code that complete replaces the // text box content only if the clipboard contains a valid double. // I'll leave improvement of this behavior as an exercise :) double value; if (double.TryParse(Clipboard.GetText(), out value)) { Text = value.ToString(); } } } } 

如果在WinForms项目中定义类,则应该能够像任何其他控件一样将其拖到窗体上。