Reputation: 379
I have a view that has 70 or so text boxes. I am using an object to store all the input so I can pass the input around my program. What I have now is a save button and when this button is pressed each text box sends it's text to the object that holds all the input.
I want to change this implementation to have the text box send it's text to the object after the cursor leaves the text box. I just don't want to write one event handler for each text box.
This site explains how to assign multiple text boxes to one event handler. This works if each event needs to do that same thing. Is their a way to assign multiple text boxes to one event handles but have each text bow upload it's contents to a different string?
I can't think of anything, so I am asking you all if you have any ideas.
Thanks,
dhoehna
Upvotes: 0
Views: 459
Reputation: 22979
You can do it easily with a dictionary:
textBox1.LostFocus += new EventHandler( textBox_LostFocus );
Dictionary<object, string> _contents = new Dictionary<object, string>( );
void textBox_LostFocus( object sender, EventArgs e ) {
_contents[ sender ] = ( sender as TextBox ).Text;
}
And to get the values as a list (in case you need it):
_contents.Select( kvp => kvp.Value ).ToList( );
Upvotes: 2
Reputation: 9648
Have a Dictionary< TextBox, string >
or even Dictionary< object, string >
and use the sender
object as the key to get the string to assign to.
Upvotes: 3