Change textbox backcolor after data change?

  • Thread starter Thread starter Dean Slindee
  • Start date Start date
D

Dean Slindee

Anyone got a quick and easy way to change the background color on a textbox
after the user has change the text value. Already have lots of forms
written, so an approach that does not depend on adding code to an event on
every textbox would be great.

Just the same, I don't see an event specifically tailored for this task
(like an OnChange event). Am I missing an obvious choice?

Thanks,
Dean S
 
Hi Dean,

once i wrote something like that, but this code is
C#, you can easily convert it to VB.NET. Sorry
Dude, i dont have VB.NET here yet, just C# and
VC.NET,...
Usage: You call "SetTextChangeHandlers()" at the end of
your class constructor and it will assignt to every textbox it
can find in the form the event handler so you dont have to
set code for any textchange event again and again. It makes
the background of a textbox red when it has no text and white
when you fill some text into it,...

//------------------CODE------------------

private void SetTextChangeHandlers() {
foreach (Control c in this.Controls)
{
try
{
if (c.GetType() == typeof(TextBox))
{
c.TextChanged += new EventHandler(c_TextChanged);
}
}
catch (Exception)
{
}
}
}
void c_TextChanged(object sender, EventArgs e)
{
TextBox t = ((TextBox)sender);
if (t.Text.Length == 0)
{
t.BackColor = Color.Red;
}
else
{
t.BackColor = Color.White;
}
}

//------------------CODE END------------------

This solution is fast, save and does not need
a lot of code,...

Regards

Kerem


--
 
Dean said:
Anyone got a quick and easy way to change the background color on a
textbox after the user has change the text value. Already have lots of
forms written, so an approach that does not depend on adding code to an
event on every textbox would be great.

Create you own Control, derived from TextBox, and add your logic to it.

Class ChangeAwareTB
Inherits TextBox

Public Sub New()
MyBase.New()

Me.BackColor = Me.DefaultBackColor

End Sub

Protected Overrides Sub OnTextChanged( _
ByVal sender as Object _
, ByVal e as EventArgs _
)

Me.BackColor = Me.ChangedBackColour

End Sub

Public Shadows Property Text() as String
Get
Return MyBase.Text
End Get
Set( Value as String )
MyBase.Text = Value
Me.BackColor = Me.DefaultBackColour
End Set
End Property

Private ReadOnly Property DefaultBackColour() as Color
Get
Return SystemColors.Window
End Get
End Property

Private ReadOnly Property ChangedBackColour() as Color
Get
Return Color.Azure
End Get
End Property

End Class

OK, you still have to revisit your existing code but only to replace the
existing TextBox controls with your new ones, something like replacing...

Private X As TextBox
. . .
X = New TextBox

.... with ...

Private X As ChangeAwareTB
. . .
X = New ChangeAwareTB

HTH,
Phill W.
 
Hi Phil,

this would be the best solution, if he likes to
extend the textbox,...

Regards

Kerem

--
 
Hi Phil,

this would be the best solution, if he likes to
extend the textbox,...

Regards

Kerem

--
-----------------------
Beste Grüsse / Best regards / Votre bien devoue
Kerem Gümrükcü
Microsoft Live Space:http://kerem-g.spaces.live.com/
Latest Open-Source Projects:http://entwicklung.junetz.de

There is a component on the code project :
http://www.thecodeproject.com/KB/vb/FormChangeControl.aspx
that raises an event whenever any control on a form is changed.
The demo app for the component changes the background colour for
textboxes that have changed.
 
Back
Top