处理在C#中粘贴事件 [英] Handle a paste event in c#
问题描述
我创建了一个静态类数字文本框,但我wan't控制哪些用户粘贴德文本框。 为了处理粘贴事件中,我使用TextChanged事件:
I've created a static class numeric Textbox but i wan't to control what the users paste in te textbox. For handling paste Event i use textchanged event:
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);
}
在另一个类我用这个静态梅索德像这样
In another class i use this static methode like this
private void tbxSigné_TextChanged(object sender, EventArgs e)
{
FiltreTbx.textChanged(e, tbxSigné, double.MinValue, double.MaxValue, @"[^\d\,\;\.\-]");
}
我wan't做的是这样的事情:
What i wan't to do is something like that:
if (match.Success)
{
textbox.Text = //Write the text before users paste in the textbox;
}
任何人都有一个想法吗?
Anyone have an idea please?
推荐答案
首先,你有没有考虑过使用的 MaskedTextBox中呢?它可以处理的字符过滤你。
First of all, have you considered using MaskedTextBox instead? It can handle the character filtering for you.
不过,为了这个练习中,你能想到沿着这条线的解决方案中。这是用法:
However, for the sake of this exercise, you could think of a solution along this line. This is the usage:
public Form1()
{
InitializeComponent();
FiltreTbx.AddTextBoxFilter(tbxSigné,
double.MinValue, double.MaxValue,
@"[^\d\,\;\.\-]");
}
本 AddTextBoxFilter
是一个新的静态方法,你只能叫一次。这将增加一个框TextChanged处理程序的文本框
。该处理器采用的是关闭存储previous 正文
在文本框中。
This AddTextBoxFilter
is a new static method, which you only call once. It will add a TextChanged handler to the TextBox
. This handler uses a closure to store the previous Text
in the text box.
您的静态方法获得一个额外的参数来传递沿着这条previous文本。
Your static method gained an extra parameter to pass along this previous 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);
}
}
我不知道是什么 tailleTextBox
是应该做的正好,你不包括源$ C $ C,但我怀疑它强制的最小值和最大值?
I'm not sure what tailleTextBox
is supposed to do exactly, you did not include that source code, but I suspect it enforces the minimum and maximum values?
。这样做的一种方式是将通过创建一个专门控制:
If you want to handle the Paste operation yourself, before it even happens, you will have to intercept the WM_PASTE
message to the text box. One way of doing is would be by creating a specialized control:
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项目,你应该能够将其拖动到你的表单像任何其他的控制。
If you define the class in your WinForms project, you should be able to drag it onto your form like any other control.
这篇关于处理在C#中粘贴事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!