By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,805 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,805 IT Pros & Developers. It's quick & easy.

Textbox background color

P: n/a
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
Jan 9 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Marco Pais" <marco.pais[at]gmail.comwrote in message
news:eg**************@TK2MSFTNGP03.phx.gbl...
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


Jan 9 '08 #2

P: n/a
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]" <mv*@spam.guard.caspershouse.comescreveu
na mensagem news:Op**************@TK2MSFTNGP03.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Marco Pais" <marco.pais[at]gmail.comwrote in message
news:eg**************@TK2MSFTNGP03.phx.gbl...
>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



Jan 9 '08 #3

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Marco Pais" <marco.pais[at]gmail.comwrote in message
news:eT**************@TK2MSFTNGP03.phx.gbl...
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]" <mv*@spam.guard.caspershouse.com>
escreveu na mensagem news:Op**************@TK2MSFTNGP03.phx.gbl...
>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]
- mv*@spam.guard.caspershouse.com

"Marco Pais" <marco.pais[at]gmail.comwrote in message
news:eg**************@TK2MSFTNGP03.phx.gbl...
>>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




Jan 9 '08 #4

P: n/a
Ok...

I'm using this:

foreach (Control control in this.Controls)

{

TextBox textBox = control as TextBox;

if (textBox != null){

textBox.Enter += delegate

{

//CHANGE BG COLOR

};

}

}

How can I know wich textbox must I change background color? How can I know
wich sender has triggered the event?

Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comescreveu
na mensagem news:ea**************@TK2MSFTNGP02.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Marco Pais" <marco.pais[at]gmail.comwrote in message
news:eT**************@TK2MSFTNGP03.phx.gbl...
>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]" <mv*@spam.guard.caspershouse.com>
escreveu na mensagem news:Op**************@TK2MSFTNGP03.phx.gbl...
>>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]
- mv*@spam.guard.caspershouse.com

"Marco Pais" <marco.pais[at]gmail.comwrote in message
news:eg**************@TK2MSFTNGP03.phx.gbl...
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




Jan 9 '08 #5

P: n/a
On Jan 9, 6:59 am, "Marco Pais" <marco.pais[at]gmail.comwrote:
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
Actually, it's even easier than the suggestions from other threads...

Instead of iterating over each control on your form, simply add one
event handler like so:
private void OnTextBoxEnter(object sender, EventArgs e)
{
((TextBox)sender).BackColor = Color.Red;
}

Then, make sure each text box subscribes to this event handler method.

Cheers,

Greg
Jan 9 '08 #6

P: n/a
Marco,

You need to adjust the code like this:

textBox.Enter += delegate(object sender, EventArgs eventArgs)
{
((TextBox) sender).BackColor = Color.Red;
};
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marco Pais" <marco.pais[at]gmail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Ok...

I'm using this:

foreach (Control control in this.Controls)

{

TextBox textBox = control as TextBox;

if (textBox != null){

textBox.Enter += delegate

{

//CHANGE BG COLOR

};

}

}

How can I know wich textbox must I change background color? How can I know
wich sender has triggered the event?

Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
escreveu na mensagem news:ea**************@TK2MSFTNGP02.phx.gbl...
>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]
- mv*@spam.guard.caspershouse.com

"Marco Pais" <marco.pais[at]gmail.comwrote in message
news:eT**************@TK2MSFTNGP03.phx.gbl...
>>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]" <mv*@spam.guard.caspershouse.com>
escreveu na mensagem news:Op**************@TK2MSFTNGP03.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Marco Pais" <marco.pais[at]gmail.comwrote in message
news:eg**************@TK2MSFTNGP03.phx.gbl...
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
>
>




Jan 9 '08 #7

P: n/a
Finally got it to work!

Thanks a lot!

Regards.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comescreveu
na mensagem news:eB****************@TK2MSFTNGP03.phx.gbl...
Marco,

You need to adjust the code like this:

textBox.Enter += delegate(object sender, EventArgs eventArgs)
{
((TextBox) sender).BackColor = Color.Red;
};
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Marco Pais" <marco.pais[at]gmail.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>Ok...

I'm using this:

foreach (Control control in this.Controls)

{

TextBox textBox = control as TextBox;

if (textBox != null){

textBox.Enter += delegate

{

//CHANGE BG COLOR

};

}

}

How can I know wich textbox must I change background color? How can I
know wich sender has triggered the event?

Thanks.

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com>
escreveu na mensagem news:ea**************@TK2MSFTNGP02.phx.gbl...
>>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]
- mv*@spam.guard.caspershouse.com

"Marco Pais" <marco.pais[at]gmail.comwrote in message
news:eT**************@TK2MSFTNGP03.phx.gbl...
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]" <mv*@spam.guard.caspershouse.com>
escreveu na mensagem news:Op**************@TK2MSFTNGP03.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com
>
"Marco Pais" <marco.pais[at]gmail.comwrote in message
news:eg**************@TK2MSFTNGP03.phx.gbl.. .
>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
>>
>>
>
>




Jan 9 '08 #8

P: n/a
If I am not mistaken, that is what the other threads suggest. =)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Greg" <gc*****@gmail.comwrote in message
news:cd**********************************@q39g2000 hsf.googlegroups.com...
On Jan 9, 6:59 am, "Marco Pais" <marco.pais[at]gmail.comwrote:
>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

Actually, it's even easier than the suggestions from other threads...

Instead of iterating over each control on your form, simply add one
event handler like so:
private void OnTextBoxEnter(object sender, EventArgs e)
{
((TextBox)sender).BackColor = Color.Red;
}

Then, make sure each text box subscribes to this event handler method.

Cheers,

Greg

Jan 9 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.