在c#中处理粘贴事件

Sef*_*Sef 6 .net c# regex static-methods paste

我已经创建了一个静态类数字文本框但我不想控制用户在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 y a 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);
    }
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

    }
Run Code Online (Sandbox Code Playgroud)

有人有想法吗?

Tho*_*rin 10

首先,您是否考虑过使用MaskedTextBox?它可以为您处理字符过滤.

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

public Form1()
{
    InitializeComponent();

    FiltreTbx.AddTextBoxFilter(tbxSigné,
                               double.MinValue, double.MaxValue,
                               @"[^\d\,\;\.\-]");
}
Run Code Online (Sandbox Code Playgroud)

AddTextBoxFilter是一个新的静态方法,您只需调用一次.它将添加一个TextChanged处理程序TextBox.此处理程序使用闭包将前一个存储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 y a 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我不确定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();
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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