PC Review


Reply
Thread Tools Rate Thread

How To: Colour Flash TextBox background?

 
 
Mr. B
Guest
Posts: n/a
 
      9th Jul 2004
While at first this may seem a simple tast, it has plagued me for a while...

What I want to do is to have the background colour of something like a TextBox
to change from (say) White to Yellow to White to Yellow and then back to White
in about 1 second or so.

This is to create something like a 'flash' (or a Pulse) for when a User clicks
on 1 of 4 Radio Buttons (which change the Text in the TextBox). So in other
words, to get the User's attention that the Text has Changed. Popup boxes are
a pain, so I don't want to go there (ie: they may click several RadioButtons).

I've tried something simple like the following to Test this out. But you do
not 'see' any background change happening. So obvious, I'm missing something
fundamental here:

Private Sub rbASME1_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles rbASME1.CheckedChanged
pltPrefx = "a"
txbScrName.Text = pltPrefx & pltTitle & pltType
Dim TimCtr, TimX As Integer
For TimX = 1 To 4
If TimX = 1 Then txbScrName.BackColor = Color.Yellow
If TimX = 2 Then txbScrName.BackColor = Color.White
If TimX = 3 Then txbScrName.BackColor = Color.Yellow
If TimX = 4 Then txbScrName.BackColor = Color.White
For TimCtr = 0 To 200000
Next 'timx
Next ' timctr
txbScrName.BackColor = Color.White
End Sub

Anyone else done something similar? I think I tried a Timer Control a while
back also without success...

Regards,

Bruce
 
Reply With Quote
 
 
 
 
Cablewizard
Guest
Posts: n/a
 
      9th Jul 2004
Mr. B,

While this isn't exactly what you want, it is similar to something I have done.
I just threw this sample together quickly with a Form, TextBox (TextBox1), and a
Button (Button1).
There very well may be a better / safer way, but I am confident it is better
than what you are trying to do.

'------------------------------------------------------------
'Will Flash the background Yellow of any control that supports the BackColor
property
'Note: No error checking implemented in this sample.
Private Sub FlashBackground(ByVal control As System.Windows.Forms.Control)
Dim origColor As System.Drawing.Color

'Store the original Background color of the control so we can restore it
origColor = control.BackColor

With control
'Set the BackColor to Yellow
.BackColor = System.Drawing.Color.Yellow
'Tell the control to redraw with the new setting
.Update()
'Wait for a short time. If we don't do this, then the update
' would be so quick we wouldn't see it.
'FYI, this will suspend the whole current GUI thread, which in this
' case is what we want.
Threading.Thread.Sleep(100)
'Restore the original BackColor
.BackColor = origColor
'Tell the control to redraw with the new setting
.Update()
End With

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
FlashBackground(TextBox1)
End Sub

'------------------------------------------------------------

Gerald

"Mr. B" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> While at first this may seem a simple tast, it has plagued me for a while...
>
> What I want to do is to have the background colour of something like a TextBox
> to change from (say) White to Yellow to White to Yellow and then back to White
> in about 1 second or so.
>
> This is to create something like a 'flash' (or a Pulse) for when a User clicks
> on 1 of 4 Radio Buttons (which change the Text in the TextBox). So in other
> words, to get the User's attention that the Text has Changed. Popup boxes are
> a pain, so I don't want to go there (ie: they may click several RadioButtons).
>
> I've tried something simple like the following to Test this out. But you do
> not 'see' any background change happening. So obvious, I'm missing something
> fundamental here:
>
> Private Sub rbASME1_CheckedChanged(ByVal sender As System.Object, _
> ByVal e As System.EventArgs) Handles rbASME1.CheckedChanged
> pltPrefx = "a"
> txbScrName.Text = pltPrefx & pltTitle & pltType
> Dim TimCtr, TimX As Integer
> For TimX = 1 To 4
> If TimX = 1 Then txbScrName.BackColor = Color.Yellow
> If TimX = 2 Then txbScrName.BackColor = Color.White
> If TimX = 3 Then txbScrName.BackColor = Color.Yellow
> If TimX = 4 Then txbScrName.BackColor = Color.White
> For TimCtr = 0 To 200000
> Next 'timx
> Next ' timctr
> txbScrName.BackColor = Color.White
> End Sub
>
> Anyone else done something similar? I think I tried a Timer Control a while
> back also without success...
>
> Regards,
>
> Bruce



 
Reply With Quote
 
Tom Spink
Guest
Posts: n/a
 
      9th Jul 2004
First off, you can make the code smaller by using the Mod statement,
Secondly, don't use loops for pauses (what if someone has a 186? or a
1millionzillion86?)

> Private Sub rbASME1_CheckedChanged(ByVal sender As System.Object, _
> ByVal e As System.EventArgs) Handles rbASME1.CheckedChanged
> pltPrefx = "a"
> txbScrName.Text = pltPrefx & pltTitle & pltType
> Dim TimCtr, TimX As Integer
> For TimX = 1 To 4
> If TimX = 1 Then txbScrName.BackColor = Color.Yellow
> If TimX = 2 Then txbScrName.BackColor = Color.White
> If TimX = 3 Then txbScrName.BackColor = Color.Yellow
> If TimX = 4 Then txbScrName.BackColor = Color.White
> For TimCtr = 0 To 200000
> Next 'timx
> Next ' timctr
> txbScrName.BackColor = Color.White
> End Sub


' ///
For TimX = 1 To 4
txtScrName.BackColor = CType(IIf((TimX Mod 2) = 0, Color.White,
Color.Yellow), System.Drawing.Color)
System.Threading.Thread.Sleep(1000)
Next

txtScrName.BackColor = Color.White
' ///

Untested, but give that a try

--
HTH,
-- Tom Spink, Über Geek

Woe be the day VBC.EXE says, "OrElse what?"

Please respond to the newsgroup,
so all can benefit
"Mr. B" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> While at first this may seem a simple tast, it has plagued me for a

while...
>
> What I want to do is to have the background colour of something like a

TextBox
> to change from (say) White to Yellow to White to Yellow and then back to

White
> in about 1 second or so.
>
> This is to create something like a 'flash' (or a Pulse) for when a User

clicks
> on 1 of 4 Radio Buttons (which change the Text in the TextBox). So in

other
> words, to get the User's attention that the Text has Changed. Popup boxes

are
> a pain, so I don't want to go there (ie: they may click several

RadioButtons).
>
> I've tried something simple like the following to Test this out. But you

do
> not 'see' any background change happening. So obvious, I'm missing

something
> fundamental here:
>
> Private Sub rbASME1_CheckedChanged(ByVal sender As System.Object, _
> ByVal e As System.EventArgs) Handles rbASME1.CheckedChanged
> pltPrefx = "a"
> txbScrName.Text = pltPrefx & pltTitle & pltType
> Dim TimCtr, TimX As Integer
> For TimX = 1 To 4
> If TimX = 1 Then txbScrName.BackColor = Color.Yellow
> If TimX = 2 Then txbScrName.BackColor = Color.White
> If TimX = 3 Then txbScrName.BackColor = Color.Yellow
> If TimX = 4 Then txbScrName.BackColor = Color.White
> For TimCtr = 0 To 200000
> Next 'timx
> Next ' timctr
> txbScrName.BackColor = Color.White
> End Sub
>
> Anyone else done something similar? I think I tried a Timer Control a

while
> back also without success...
>
> Regards,
>
> Bruce



 
Reply With Quote
 
Tom Spink
Guest
Posts: n/a
 
      9th Jul 2004
Shoot... something that just crossed my mind is that you may need an
Application.DoEvents() in there to repaint the textbox.... If it doesn't
work first time, add this line before the
System.Threading.Thread.Sleep(1000) line:

Application.DoEvents()

--
HTH,
-- Tom Spink, Über Geek

Woe be the day VBC.EXE says, "OrElse what?"

Please respond to the newsgroup,
so all can benefit
"Mr. B" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> While at first this may seem a simple tast, it has plagued me for a

while...
>
> What I want to do is to have the background colour of something like a

TextBox
> to change from (say) White to Yellow to White to Yellow and then back to

White
> in about 1 second or so.
>
> This is to create something like a 'flash' (or a Pulse) for when a User

clicks
> on 1 of 4 Radio Buttons (which change the Text in the TextBox). So in

other
> words, to get the User's attention that the Text has Changed. Popup boxes

are
> a pain, so I don't want to go there (ie: they may click several

RadioButtons).
>
> I've tried something simple like the following to Test this out. But you

do
> not 'see' any background change happening. So obvious, I'm missing

something
> fundamental here:
>
> Private Sub rbASME1_CheckedChanged(ByVal sender As System.Object, _
> ByVal e As System.EventArgs) Handles rbASME1.CheckedChanged
> pltPrefx = "a"
> txbScrName.Text = pltPrefx & pltTitle & pltType
> Dim TimCtr, TimX As Integer
> For TimX = 1 To 4
> If TimX = 1 Then txbScrName.BackColor = Color.Yellow
> If TimX = 2 Then txbScrName.BackColor = Color.White
> If TimX = 3 Then txbScrName.BackColor = Color.Yellow
> If TimX = 4 Then txbScrName.BackColor = Color.White
> For TimCtr = 0 To 200000
> Next 'timx
> Next ' timctr
> txbScrName.BackColor = Color.White
> End Sub
>
> Anyone else done something similar? I think I tried a Timer Control a

while
> back also without success...
>
> Regards,
>
> Bruce



 
Reply With Quote
 
One Handed Man \( OHM - Terry Burns \)
Guest
Posts: n/a
 
      9th Jul 2004
Wrap the timer in a new class and overload the start to pass the control
you wan to affect. Then use a control property on the wrapped class to store
the reference to the control being processed so that your handler can alter
the right textbox. When your done, just stop the timer

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Mr. B" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> While at first this may seem a simple tast, it has plagued me for a

while...
>
> What I want to do is to have the background colour of something like a

TextBox
> to change from (say) White to Yellow to White to Yellow and then back to

White
> in about 1 second or so.
>
> This is to create something like a 'flash' (or a Pulse) for when a User

clicks
> on 1 of 4 Radio Buttons (which change the Text in the TextBox). So in

other
> words, to get the User's attention that the Text has Changed. Popup boxes

are
> a pain, so I don't want to go there (ie: they may click several

RadioButtons).
>
> I've tried something simple like the following to Test this out. But you

do
> not 'see' any background change happening. So obvious, I'm missing

something
> fundamental here:
>
> Private Sub rbASME1_CheckedChanged(ByVal sender As System.Object, _
> ByVal e As System.EventArgs) Handles rbASME1.CheckedChanged
> pltPrefx = "a"
> txbScrName.Text = pltPrefx & pltTitle & pltType
> Dim TimCtr, TimX As Integer
> For TimX = 1 To 4
> If TimX = 1 Then txbScrName.BackColor = Color.Yellow
> If TimX = 2 Then txbScrName.BackColor = Color.White
> If TimX = 3 Then txbScrName.BackColor = Color.Yellow
> If TimX = 4 Then txbScrName.BackColor = Color.White
> For TimCtr = 0 To 200000
> Next 'timx
> Next ' timctr
> txbScrName.BackColor = Color.White
> End Sub
>
> Anyone else done something similar? I think I tried a Timer Control a

while
> back also without success...
>
> Regards,
>
> Bruce



 
Reply With Quote
 
One Handed Man \( OHM - Terry Burns \)
Guest
Posts: n/a
 
      9th Jul 2004
There ya go MR. B., a more comprehensive answer ! - (Tested I Might Add )
----------------------------------------------------------------------------
-

Public Class xTimer
Inherits Timer

Public Sub New()
MyBase.New()
End Sub

Private m_control As Control
Public Property xControl() As Control
Get
Return m_control
End Get
Set(ByVal Value As Control)
m_control = Value
End Set
End Property

End Class


// FORM
Private WithEvents MyTimer As New xTimer
Private FlipBackColor As Boolean = True

Private Sub MyTimer_Tick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyTimer.Tick
Dim tim As xTimer = DirectCast(sender, xTimer)
Dim xText As TextBox
Try
xText = tim.xControl()
If FlipBackColor Then
xText.BackColor = Color.Red
FlipBackColor = False
Else
xText.BackColor = Color.White
FlipBackColor = True
End If
Catch ex As Exception
'TODO:
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Try
MyTimer.xControl = TextBox1
MyTimer.Interval = 250
MyTimer.Start()
Catch ex As Exception
'TODO:
End Try

End Sub

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Mr. B" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> While at first this may seem a simple tast, it has plagued me for a

while...
>
> What I want to do is to have the background colour of something like a

TextBox
> to change from (say) White to Yellow to White to Yellow and then back to

White
> in about 1 second or so.
>
> This is to create something like a 'flash' (or a Pulse) for when a User

clicks
> on 1 of 4 Radio Buttons (which change the Text in the TextBox). So in

other
> words, to get the User's attention that the Text has Changed. Popup boxes

are
> a pain, so I don't want to go there (ie: they may click several

RadioButtons).
>
> I've tried something simple like the following to Test this out. But you

do
> not 'see' any background change happening. So obvious, I'm missing

something
> fundamental here:
>
> Private Sub rbASME1_CheckedChanged(ByVal sender As System.Object, _
> ByVal e As System.EventArgs) Handles rbASME1.CheckedChanged
> pltPrefx = "a"
> txbScrName.Text = pltPrefx & pltTitle & pltType
> Dim TimCtr, TimX As Integer
> For TimX = 1 To 4
> If TimX = 1 Then txbScrName.BackColor = Color.Yellow
> If TimX = 2 Then txbScrName.BackColor = Color.White
> If TimX = 3 Then txbScrName.BackColor = Color.Yellow
> If TimX = 4 Then txbScrName.BackColor = Color.White
> For TimCtr = 0 To 200000
> Next 'timx
> Next ' timctr
> txbScrName.BackColor = Color.White
> End Sub
>
> Anyone else done something similar? I think I tried a Timer Control a

while
> back also without success...
>
> Regards,
>
> Bruce



 
Reply With Quote
 
Cablewizard
Guest
Posts: n/a
 
      9th Jul 2004
Ok, here is an update that will do what you want put into your context.
See previous post for original comments.

'------------------------------------------------------------
'Will Flash the background Yellow of any control that supports
' the BackColor property
Private Sub FlashBackground(ByVal control As System.Windows.Forms.Control)
Dim origColor As System.Drawing.Color

Try
'Store the original Background color of the control so we can
restore it
origColor = control.BackColor
With control

For flashCount As Integer = 1 To 2
.BackColor = System.Drawing.Color.Yellow
.Update()
Threading.Thread.Sleep(200)
.BackColor = System.Drawing.Color.White
'If your original BackColor is not White or Yellow already,
' then maybe try the following, as it is a little prettier
'.BackColor = origColor
.Update()
Threading.Thread.Sleep(200)
Next flashCount

'Not necessary if you use the origColor above
.BackColor = origColor
.Update()
End With
Catch ex As Exception
'TODO:
End Try

End Sub


Private Sub rbASME1_CheckedChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles rbASME1.CheckedChanged

pltPrefx = "a"
txbScrName.Text = pltPrefx & pltTitle & pltType

FlashBackground(txbScrName)

End Sub

'------------------------------------------------------------

Of course you could make the flash interval, color, etc. anything you want.
You could even pass them in as parameters to the Sub for complete control.

FYI: One Handed Man's solution has the benefit of you being able to just
set it off and let it run for continual flashing if desired.

Gerald

>
> "Mr. B" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > While at first this may seem a simple tast, it has plagued me for a while...
> >
> > What I want to do is to have the background colour of something like a

TextBox
> > to change from (say) White to Yellow to White to Yellow and then back to

White
> > in about 1 second or so.
> >
> > This is to create something like a 'flash' (or a Pulse) for when a User

clicks
> > on 1 of 4 Radio Buttons (which change the Text in the TextBox). So in other
> > words, to get the User's attention that the Text has Changed. Popup boxes

are
> > a pain, so I don't want to go there (ie: they may click several

RadioButtons).
> >
> > I've tried something simple like the following to Test this out. But you do
> > not 'see' any background change happening. So obvious, I'm missing

something
> > fundamental here:
> >
> > Private Sub rbASME1_CheckedChanged(ByVal sender As System.Object, _
> > ByVal e As System.EventArgs) Handles rbASME1.CheckedChanged
> > pltPrefx = "a"
> > txbScrName.Text = pltPrefx & pltTitle & pltType
> > Dim TimCtr, TimX As Integer
> > For TimX = 1 To 4
> > If TimX = 1 Then txbScrName.BackColor = Color.Yellow
> > If TimX = 2 Then txbScrName.BackColor = Color.White
> > If TimX = 3 Then txbScrName.BackColor = Color.Yellow
> > If TimX = 4 Then txbScrName.BackColor = Color.White
> > For TimCtr = 0 To 200000
> > Next 'timx
> > Next ' timctr
> > txbScrName.BackColor = Color.White
> > End Sub
> >
> > Anyone else done something similar? I think I tried a Timer Control a while
> > back also without success...
> >
> > Regards,
> >
> > Bruce

>
>



 
Reply With Quote
 
Herfried K. Wagner [MVP]
Guest
Posts: n/a
 
      9th Jul 2004
* "One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> scripsit:
> Public Class xTimer
> Inherits Timer
>
> Public Sub New()
> MyBase.New()
> End Sub
>
> Private m_control As Control
> Public Property xControl() As Control
> Get
> Return m_control
> End Get
> Set(ByVal Value As Control)
> m_control = Value
> End Set
> End Property
>
> End Class
>
>
> // FORM
> Private WithEvents MyTimer As New xTimer
> Private FlipBackColor As Boolean = True
>
> Private Sub MyTimer_Tick(ByVal sender As Object, ByVal e As
> System.EventArgs) Handles MyTimer.Tick
> Dim tim As xTimer = DirectCast(sender, xTimer)
> Dim xText As TextBox
> Try
> xText = tim.xControl()
> If FlipBackColor Then
> xText.BackColor = Color.Red
> FlipBackColor = False
> Else
> xText.BackColor = Color.White
> FlipBackColor = True
> End If
> Catch ex As Exception
> 'TODO:
> End Try


What exceptions are you expecting here?

BTW: I don't see any advantages in adding the reference to the control to the timer, and then placing the
'Tick' event handler to the form. Instead, I would extend the textbox,
instantiate the timer in the extended textbox to keep the stuff outside
the form's implementation (better encapsulation).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
 
Reply With Quote
 
One Handed Man \( OHM - Terry Burns \)
Guest
Posts: n/a
 
      9th Jul 2004
No. Many textBoxes, 1 timer

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Herfried K. Wagner [MVP]" <hirf-spam-me-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> * "One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> scripsit:
> > Public Class xTimer
> > Inherits Timer
> >
> > Public Sub New()
> > MyBase.New()
> > End Sub
> >
> > Private m_control As Control
> > Public Property xControl() As Control
> > Get
> > Return m_control
> > End Get
> > Set(ByVal Value As Control)
> > m_control = Value
> > End Set
> > End Property
> >
> > End Class
> >
> >
> > // FORM
> > Private WithEvents MyTimer As New xTimer
> > Private FlipBackColor As Boolean = True
> >
> > Private Sub MyTimer_Tick(ByVal sender As Object, ByVal e As
> > System.EventArgs) Handles MyTimer.Tick
> > Dim tim As xTimer = DirectCast(sender, xTimer)
> > Dim xText As TextBox
> > Try
> > xText = tim.xControl()
> > If FlipBackColor Then
> > xText.BackColor = Color.Red
> > FlipBackColor = False
> > Else
> > xText.BackColor = Color.White
> > FlipBackColor = True
> > End If
> > Catch ex As Exception
> > 'TODO:
> > End Try

>
> What exceptions are you expecting here?
>
> BTW: I don't see any advantages in adding the reference to the control to

the timer, and then placing the
> 'Tick' event handler to the form. Instead, I would extend the textbox,
> instantiate the timer in the extended textbox to keep the stuff outside
> the form's implementation (better encapsulation).
>
> --
> Herfried K. Wagner [MVP]
> <URL:http://dotnet.mvps.org/>



 
Reply With Quote
 
One Handed Man \( OHM - Terry Burns \)
Guest
Posts: n/a
 
      10th Jul 2004
I normally would not have put the Try/Catch here, but when I was debugging I
was getting some exceptions.

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing

"Herfried K. Wagner [MVP]" <hirf-spam-me-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> * "One Handed Man \( OHM - Terry Burns \)" <news.microsoft.com> scripsit:
> > Public Class xTimer
> > Inherits Timer
> >
> > Public Sub New()
> > MyBase.New()
> > End Sub
> >
> > Private m_control As Control
> > Public Property xControl() As Control
> > Get
> > Return m_control
> > End Get
> > Set(ByVal Value As Control)
> > m_control = Value
> > End Set
> > End Property
> >
> > End Class
> >
> >
> > // FORM
> > Private WithEvents MyTimer As New xTimer
> > Private FlipBackColor As Boolean = True
> >
> > Private Sub MyTimer_Tick(ByVal sender As Object, ByVal e As
> > System.EventArgs) Handles MyTimer.Tick
> > Dim tim As xTimer = DirectCast(sender, xTimer)
> > Dim xText As TextBox
> > Try
> > xText = tim.xControl()
> > If FlipBackColor Then
> > xText.BackColor = Color.Red
> > FlipBackColor = False
> > Else
> > xText.BackColor = Color.White
> > FlipBackColor = True
> > End If
> > Catch ex As Exception
> > 'TODO:
> > End Try

>
> What exceptions are you expecting here?
>
> BTW: I don't see any advantages in adding the reference to the control to

the timer, and then placing the
> 'Tick' event handler to the form. Instead, I would extend the textbox,
> instantiate the timer in the extended textbox to keep the stuff outside
> the form's implementation (better encapsulation).
>
> --
> Herfried K. Wagner [MVP]
> <URL:http://dotnet.mvps.org/>



 
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
How do I change background colour of textbox at runtime Ascorpius Microsoft Access Form Coding 1 23rd Feb 2009 12:43 PM
Code to change background colour of textbox on doubleclick Swannybuck Microsoft Access 6 14th Mar 2008 05:58 PM
how do you fill a textbox with a certain colour(as background)? =?Utf-8?B?Z2VvcmdlX2IxMDA0?= Microsoft Word Document Management 1 10th Apr 2007 07:14 PM
Specifying the background colour of an <asp:TextBox> also changes its border style Mark Rae Microsoft ASP .NET 1 17th Jan 2007 08:39 PM
How To: Colour Flash TextBox background? Mr. B Microsoft Dot NET 11 10th Jul 2004 08:37 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:27 PM.