473,383 Members | 1,892 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,383 software developers and data experts.

Change textbox backcolor after data change?

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

Similar topics

3
by: Steve Le Monnier | last post by:
When you make a textbox control disabled ( txtText1.Enabled = false; ) You're left with grey text on a greyer background, which is not ideal. The Read-Only property is no use to me as I need to...
3
by: jm | last post by:
I have a repeater and based upon some database results want to change the backcolor or font color of the row at my databind statement. I have done this before with a datagrid: e.Item.BackColor...
1
by: Mike | last post by:
On my update screen i want to change the textbox color to Yellow only if the field is editable. How can i do that within code? thx
0
by: bholaday | last post by:
Hi, I have a simple textbox control on my aspx. I want to change the BackColor of that textbox when a button is triggered through the use of a function in my code behind file. My code does not...
1
by: zand | last post by:
how can i change textbox alignment in web form in vb.net . i wanna to have righttoleft textbox
0
by: =?Utf-8?B?QnJpYW5ESA==?= | last post by:
Hi I am trying to find a way to change the background color of a cell (flash or blink) when its value has changed (by user or data update). I need to be able to do this for more than one cell...
3
by: Syoam4ka | last post by:
Hi, I want to change the backcolor of my button every second ,for example red, after a second blue, after a second re, then blue and so on...i need it in a web form not in a windows form(in winform...
2
by: wildman | last post by:
RE: Gridview textbox has data check without postback.. javascript? I have a gridview with a textbox. I can set the textbox to autopostback and check for a value in a prerender event to decide if...
12
by: jamesnkk | last post by:
Hi, I am new to visual basic 6, In a form I have many 20 textboxes such as If (texbox1.Text < 2.85) Or (textbox1.Text > 3.15) Then textbox1.Text = "" Else ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.