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

Change textbox backcolor after data change?

P: n/a
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

Dec 13 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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
--
-----------------------
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
-----------------------
"This reply is provided as is, without warranty express or implied."
Dec 13 '07 #2

P: n/a
Dean Slindee wrote:
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.
Dec 13 '07 #3

P: n/a
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
-----------------------
"This reply is provided as is, without warranty express or implied."
Dec 13 '07 #4

P: n/a
On Dec 13, 6:40 pm, "Kerem Gümrükcü" <kareem...@hotmail.comwrote:
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
-----------------------
"This reply is provided as is, without warranty express or implied."
There is a component on the code project :
http://www.thecodeproject.com/KB/vb/...geControl.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.
Dec 14 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.