Click to See Complete Forum and Search --> : Textbox Event
Bongo
Nov 15th, 2005, 12:24 AM
Greetings, I have a form with 20 Textboxes. When the user leaves the textbox and has changed anything an update has to be written to the DB.
Is it possible to use only one Eventhandler to cover all Textboxes or do I realy need for every Textbox on this form an own Eventhandler?
Thankx in advance
popskie
Nov 15th, 2005, 12:37 AM
hi,
In my exp. yes u can.
in any event you put this code. Instead of using each textbox name.
((TextBox)sender).Text
Bongo
Nov 15th, 2005, 12:42 AM
@popskie:
thanks - I will try it
conipto
Nov 15th, 2005, 02:05 AM
You can make the method work for different controls by clicking the events button on the form designer properties, and choosing the method from the drop down list.
note, that, If you look under "Windows Form Designer code" you will see the IDE adds the handler by using this method:
this.textBox3.Validating += new System.ComponentModel.CancelEventHandler(this.multivalidator);
Which you could also use yourself programatically at runtime to add event handlers.
Bill
mendhak
Nov 15th, 2005, 09:13 AM
In the designer code, set all of them to have the same event handler, like this for example:
this.textBox1.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
this.textBox3.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
Then you can have a look in the event:
private void textBox2_TextChanged(object sender, System.EventArgs e)
{
TextBox txtTemp = (TextBox)sender;
MessageBox.Show(txtTemp.Name + " has changed!");
}
HTH
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.