Marco,
When cycling through the Controls collection, you want to do
something like this:
foreach (Control control in myForm.Controls)
{
// The textbox control.
TextBox textBox = control as TextBox;
// If not null, set the handler.
if (textBox != null)
{
// Set the handler.
textBox.Enter += OnTextBoxEnter;
}
}
Of course OnTextBoxEnter is the method that sets the color to red.
You could use anonymous methods in C# 2.0 or a lambda expression in C#
3.0 to make it even simpler.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
"Marco Pais" <marco.pais[at]gmail.com> wrote in message
Hi there.
Thanks for the reply.
I'm not sure I understand "...just use the as operator...".
I think I must use something with "+=" bu I don't know how...
Thanks
"Nicholas Paldino [.NET/C# MVP]" <
[email protected]>
escreveu na mensagem Marco,
First, you would change your code to this:
((TextBox) sender).BackColor = Color.Red;
Then, on your form, you would cycle through all the TextBox
controls (the Controls collection will help you with this, just use
the as operator to determine if the control is a textbox) and then
attach that method to the Enter event handler.
You might want to make sure you attach another event handler to
change the color back when the textbox loses focus.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
"Marco Pais" <marco.pais[at]gmail.com> wrote in message
Hi there.
How can I change the background color of a textbox when it gets the
focus?
I can handle the "Enter" event and do this
private void txtDummie_Enter(object sender, EventArgs e) {
txtDummie.BackColor=Color.Red;
}
But how can I use this code to work with all textboxes in form? How
can I use "sender" argument?
Thanks in advance.
Regards,
Marco