PC Review


Reply
Thread Tools Rate Thread

Textbox background color

 
 
Marco Pais
Guest
Posts: n/a
 
      9th Jan 2008
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


 
Reply With Quote
 
 
 
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      9th Jan 2008
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 Removed)

"Marco Pais" <marco.pais[at]gmail.com> wrote in message
news:(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
 
 
 
Marco Pais
Guest
Posts: n/a
 
      9th Jan 2008
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]" <(E-Mail Removed)> escreveu
na mensagem news:(E-Mail Removed)...
> 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 Removed)
>
> "Marco Pais" <marco.pais[at]gmail.com> wrote in message
> news:(E-Mail Removed)...
>> 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
>>
>>

>
>



 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      9th Jan 2008
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 Removed)

"Marco Pais" <marco.pais[at]gmail.com> wrote in message
news:(E-Mail Removed)...
> 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]" <(E-Mail Removed)>
> escreveu na mensagem news:(E-Mail Removed)...
>> 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 Removed)
>>
>> "Marco Pais" <marco.pais[at]gmail.com> wrote in message
>> news:(E-Mail Removed)...
>>> 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
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
Marco Pais
Guest
Posts: n/a
 
      9th Jan 2008
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]" <(E-Mail Removed)> escreveu
na mensagem news:(E-Mail Removed)...
> 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 Removed)
>
> "Marco Pais" <marco.pais[at]gmail.com> wrote in message
> news:(E-Mail Removed)...
>> 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]" <(E-Mail Removed)>
>> escreveu na mensagem news:(E-Mail Removed)...
>>> 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 Removed)
>>>
>>> "Marco Pais" <marco.pais[at]gmail.com> wrote in message
>>> news:(E-Mail Removed)...
>>>> 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
>>>>
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      9th Jan 2008
On Jan 9, 6:59 am, "Marco Pais" <marco.pais[at]gmail.com> wrote:
> 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
 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      9th Jan 2008
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]
- (E-Mail Removed)

"Marco Pais" <marco.pais[at]gmail.com> wrote in message
news:%(E-Mail Removed)...
> 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]" <(E-Mail Removed)>
> escreveu na mensagem news:(E-Mail Removed)...
>> 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 Removed)
>>
>> "Marco Pais" <marco.pais[at]gmail.com> wrote in message
>> news:(E-Mail Removed)...
>>> 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]" <(E-Mail Removed)>
>>> escreveu na mensagem news:(E-Mail Removed)...
>>>> 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 Removed)
>>>>
>>>> "Marco Pais" <marco.pais[at]gmail.com> wrote in message
>>>> news:(E-Mail Removed)...
>>>>> 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
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
Marco Pais
Guest
Posts: n/a
 
      9th Jan 2008
Finally got it to work!

Thanks a lot!

Regards.

"Nicholas Paldino [.NET/C# MVP]" <(E-Mail Removed)> escreveu
na mensagem news:eB%(E-Mail Removed)...
> 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]
> - (E-Mail Removed)
>
> "Marco Pais" <marco.pais[at]gmail.com> wrote in message
> news:%(E-Mail Removed)...
>> 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]" <(E-Mail Removed)>
>> escreveu na mensagem news:(E-Mail Removed)...
>>> 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 Removed)
>>>
>>> "Marco Pais" <marco.pais[at]gmail.com> wrote in message
>>> news:(E-Mail Removed)...
>>>> 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]" <(E-Mail Removed)>
>>>> escreveu na mensagem news:(E-Mail Removed)...
>>>>> 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 Removed)
>>>>>
>>>>> "Marco Pais" <marco.pais[at]gmail.com> wrote in message
>>>>> news:(E-Mail Removed)...
>>>>>> 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
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      9th Jan 2008
If I am not mistaken, that is what the other threads suggest. =)


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Greg" <(E-Mail Removed)> wrote in message
news:cd462477-7d03-4878-b72a-(E-Mail Removed)...
> On Jan 9, 6:59 am, "Marco Pais" <marco.pais[at]gmail.com> wrote:
>> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
custom color for font color and or background shading color Ben Microsoft Excel Programming 2 5th May 2010 02:32 PM
color transition (color changes from dark color to light color) color transition Microsoft Powerpoint 2 21st Nov 2009 01:21 PM
Change textbox color based on matching value in different textbox CompleteNewb Microsoft Access Form Coding 4 16th Nov 2007 11:41 PM
Fill Color/ Background Color/Tab Color M.Cave Microsoft Excel Misc 2 11th Oct 2004 02:10 AM
Uncontrollable textbox (input type=text) background color Andrew Windows XP Internet Explorer 1 17th Oct 2003 06:29 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:59 PM.