473,403 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Textbox background color

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
8 36851
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Mr. B | last post by:
While at first this may seem a simple tast, it has plagued me for a while... What I want to do is to have the background colour of something like a TextBox to change from (say) White to Yellow to...
2
by: mr_burns | last post by:
Hi there, How do I change the background color of a textbox when it is clicked on? Cheers Burnsy
3
by: Roberto Castro | last post by:
Hello! I have been assigned for the first time an adp Access project and so far I have managed to make the changes needed for some requirements. However, I am struggling to find the place where...
3
by: Steve | last post by:
I have windows form with the few TextBox controls. Under certain conditions I disable or enable this controls by setting Enable property to true or false: For example: this.m_txtIP.Enabled =...
4
by: Craig Duffy | last post by:
When I load up my page, some of the text boxes show up with a light yellow color. I can't change it to any other color or even set it transparent. Anybody know what's going on? The background...
3
by: Wayne Wengert | last post by:
I am treying the change the background color of textboxes that have invalid data. I am trying the code shown below but when I run it, I get an exception? Any thoughts on why this doesn't work? ...
5
by: Mark Sandfox | last post by:
How do you control the color of a textbox background color? I have an issue that when a user is viewing a form in the same version of IE 6 and operating system but a differenet computer they get a...
1
by: da5namroi | last post by:
One of my my textboxes has a nice white background when running on an XP machine, but becomes dark blak when running on a Vista. Does anybody know how I can fix this? Thank you.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.