Textbox background color

M

Marco Pais

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
M

Marco Pais

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 said:
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 said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

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 said:
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 said:
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 said:
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
 
M

Marco Pais

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 said:
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 said:
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 said:
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
 
G

Greg

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
 
N

Nicholas Paldino [.NET/C# MVP]

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 address removed)

Marco Pais said:
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 said:
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 said:
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
 
M

Marco Pais

Finally got it to work!

Thanks a lot!

Regards.

Nicholas Paldino said:
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 address removed)

Marco Pais said:
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 said:
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
 
N

Nicholas Paldino [.NET/C# MVP]

If I am not mistaken, that is what the other threads suggest. =)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top