Handle C # Insertion Event

I created a text box of a static class, but I can’t control what users paste into the text box. To handle the insertion of an Event, I use an event with a modified text:

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); } 

In another class, I use this static method similar to this

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

What I do not need to do is something like this:

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

Does anyone have an idea?

+6
source share
1 answer

First of all, have you considered MaskedTextBox instead? It can handle character filtering for you.

However, for the sake of this exercise, you might think of a solution along this line. This usage:

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

This AddTextBoxFilter is a new static method that you call only once. It will add a TextChanged handler to the TextBox . This handler uses closure to store the previous Text in a text field.

Your static method has received an additional parameter to go through 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 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); } } 

I'm not sure what exactly tailleTextBox should do exactly, you did not include this source code, but I suspect that it applies the minimum and maximum values?

Alternative solution

If you want to process the Insert operation yourself before this happens, you will have to intercept the WM_PASTE message in the text box. One way to do this is to create a custom 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(); } } } } 

If you define a class in a WinForms project, you can drag it into your form like any other control.

+10
source

All Articles