Se traduci tutto in italiano, forse riusciamo ad aiutarti
Ti spiego come funzionano gli eventi.
Diciamo che vogliamo creare una nuova textbox che mi avverta tramite un evento TextTooLong quando il numero di caratteri al suo interno e' maggiore di 5 e mi ritorni nel parametro dell'evento il numero di caratteri presente nella textbox.
Mi definisco la classe derivata da EventArgs che conterra' il numero di caratteri presenti nella textbox.
codice:
public class MyTextBoxEventArgs : EventArgs
{
public int textLenght = 0;
public MyTextBoxEventArgs(int txtlen)
{
textLenght = txtlen;
}
}
Quindi derivo da TextBox la mia MyTextBox in modo da implementare gli eventi
codice:
public class MyTextBox : TextBox
{
public delegate void TextTooLongEventHandler(object sender, MyTextBoxEventArgs e);
public event TextTooLongEventHandler TextTooLong;
public MyTextBox() : base()
{
this.TextChanged+=new EventHandler(MyTextBox_TextChanged);
}
private void MyTextBox_TextChanged(object sender, EventArgs e)
{
if (this.TextLength > 5)
{
if (TextTooLong != null)
{
MyTextBoxEventArgs myEvent = new MyTextBoxEventArgs(this.TextLength);
TextTooLong(this, myEvent);
}
}
}
}
Innanzitutto ho definito un nuovo EventHandler che prenda come parametri l'oggetto sender, e come parametro la classe MyTextBoxEventArgs .
Quindi ho definito un nuovo evento chiamato TextTooLong . Questo e' il nome dell'evento visto esternamente .
Nel costruttore della classe ho intercettato l'evento di TextChanged ;
L'implementazione dell'evento controlla se il numero di caratteri sia maggiore di 5 e che l'utente, abbia gestito l'evento TextTooLong .
Se entrambi sono verificati, non mi resta che definire l'argomento, passandogli il numero di caratteri presente nella textbox ed invocare l'evento.
Per utilizzare l'evento, e' sufficiente creare un oggetto di tipo myTextBox ed intercettare l'evento di TextTooLong .
codice:
this.textBox1.Location = new System.Drawing.Point(32, 64);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
this.textBox1.TextTooLong+=new MyTextBox.TextTooLongEventHandler(textBox1_TextTooLong);
Ed implementarlo
codice:
private void textBox1_TextTooLong(object sender, MyTextBoxEventArgs e)
{
string mess = string.Format("Text if too long: {0} characters", e.textLenght);
MessageBox.Show(mess, "Error");
}
Di Seguito tutta l'applicazione.
codice:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication2
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private MyTextBox textBox1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.textBox1 = new WindowsApplication2.MyTextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 64);
this.textBox1.Name = "textBox1";
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
this.textBox1.TextTooLong += new WindowsApplication2.MyTextBox.TextTooLongEventHandler(this.textBox1_TextTooLong);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void textBox1_TextTooLong(object sender, MyTextBoxEventArgs e)
{
string mess = string.Format("Text if too long: {0} characters", e.textLenght);
MessageBox.Show(mess, "Error");
}
}
public class MyTextBoxEventArgs : EventArgs
{
public int textLenght = 0;
public MyTextBoxEventArgs(int txtlen)
{
textLenght = txtlen;
}
}
public class MyTextBox : TextBox
{
public delegate void TextTooLongEventHandler(object sender, MyTextBoxEventArgs e);
public event TextTooLongEventHandler TextTooLong;
public MyTextBox() : base()
{
this.TextChanged+=new EventHandler(MyTextBox_TextChanged);
}
private void MyTextBox_TextChanged(object sender, EventArgs e)
{
if (this.TextLength > 5)
{
if (TextTooLong != null)
{
MyTextBoxEventArgs myEvent = new MyTextBoxEventArgs(this.TextLength);
TextTooLong(this, myEvent);
}
}
}
}
}
Spero che ora ti sia tutto piu' chiaro.
Mauro