-
Feb 2nd, 2018, 03:30 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Is it possible to make a textbox blinking with backColor?
Is it possible to make a textbox blinking with backColor to attract the attention?
-
Feb 2nd, 2018, 03:38 PM
#2
Re: Is it possible to make a textbox blinking with backColor?
Just change the backcolor with a timer.
-
Feb 4th, 2018, 06:21 PM
#3
Re: Is it possible to make a textbox blinking with backColor?
As suggested, use a Timer. Basically, any time you want to do something after a specific amount of time, use a Timer. If you only want to do it once, Stop the Timer on the first Tick event, otherwise let it keep running. In your case, you could use an If...Else block to alternate the BackColor, or use an If operator, e.g.
vb.net Code:
TextBox1.BackColor = If(TextBox1.BackColor = Color.Red, Color.Yellow, Color.Red)
-
Feb 5th, 2018, 10:41 AM
#4
Re: Is it possible to make a textbox blinking with backColor?
I would caution against quite the blink that JMC is showing. I'm known for using some garish colors, but even I wouldn't flash Red/Yellow. Try flashing two shades of the same color, like Red/Pink, or something like that.
My usual boring signature: Nothing
-
Feb 5th, 2018, 10:55 AM
#5
Re: Is it possible to make a textbox blinking with backColor?
Originally Posted by Shaggy Hiker
I would caution against quite the blink that JMC is showing. I'm known for using some garish colors, but even I wouldn't flash Red/Yellow. Try flashing two shades of the same color, like Red/Pink, or something like that.
How do you expect to get people's attention if you're not offending their senses?
-
Feb 5th, 2018, 12:19 PM
#6
Re: Is it possible to make a textbox blinking with backColor?
Well, if you want to get the user's attention the ErrorProvider control's a little more idiomatic.
Flashing red/yellow or some other high-contrast colors can be dangerous depending on the rate. There are people with light and color sensitivities and at high cycle rates you could trigger a seizure. Not to mention it's just plain ugly unless you get some feedback from a designer on which colors aren't garish.
Considering that ErrorProvider exists and it's more like what people are used to seeing, that should be used instead.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Feb 5th, 2018, 12:30 PM
#7
Re: Is it possible to make a textbox blinking with backColor?
Originally Posted by jmcilhinney
How do you expect to get people's attention if you're not offending their senses?
I play dirty noises.
Actually, I did do something like that. I wrote a program that was used in a loud and busy environment. A certain event needed to get everybodies attention right away, but it wasn't an emergency exactly (it dealt with fish, after all). So, the screen itself flashed three times and Homer Simpson's voice said "Hey, what the hell was that?". It worked pretty well. I wanted to use a Monty Python sound clip of "fish!", but it was too short to really work well.
@Sitten: I understood the OP to be just trying to draw the users attention to 'the first control they needed to address.', in which case the error provider doesn't strike me as quite as good. However, I just went and looked at it. It's got more versatility than I realized. As long as you can set the icon, which you can, it does seem like a viable alternative.
My usual boring signature: Nothing
-
Feb 6th, 2018, 12:22 PM
#8
Thread Starter
Hyperactive Member
Re: Is it possible to make a textbox blinking with backColor?
Thanks to all of you. I just want my textbox to blink for 3 seconds. How can I?
-
Feb 6th, 2018, 12:42 PM
#9
Re: Is it possible to make a textbox blinking with backColor?
1) Add a timer to the form with an interval set to whatever blink rate you want. Better make it around 500 to start with, and go from there.
2) Add an integer variable to the form.
3) In the timer tick event, add the code that JMC showed in #3.
4) Also in the tick event, increment the integer variable from step #2.
5) When the integer variable reaches 6, then stop the timer. If you have an interval of 500 ms, then this will count up six intervals, and stop it on the seventh.
My usual boring signature: Nothing
-
Feb 6th, 2018, 02:32 PM
#10
Thread Starter
Hyperactive Member
Re: Is it possible to make a textbox blinking with backColor?
Got this code but doesn't work.
Code:
Private Sub Timer1_Timer()
Static bDummy As Boolean
Static i As Long
i = i + 1
If i < 100 Then
bDummy = Not bDummy
If bDummy Then
TextBox1.BackColor = Color.Red
Else
TextBox1.BackColor = Color.Yellow
End If
Else
Timer1.Enabled = False
i = 0
End If
End Sub
Last edited by VS2013; Feb 6th, 2018 at 02:52 PM.
-
Feb 6th, 2018, 02:53 PM
#11
Re: Is it possible to make a textbox blinking with backColor?
Here is a way to do what you asked using a Task. Included is some contrast color code.
The example uses two textboxes and one button.
Code:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FlashTB(TextBox1, Color.Red, 4000)
FlashTB(TextBox2, Color.Blue, 2000)
End Sub
Private Sub FlashTB(txtBox As TextBox, BkgndColor As Color, HowLongMS As Integer)
Dim t As Task
Const flshTM As Integer = 250
t = Task.Run(Sub()
Dim stpw As Stopwatch = Stopwatch.StartNew
Dim tb As TextBox
Dim curBack As Color
Dim curFore As Color
Dim newBack As Color = BkgndColor
Dim tmMS As Integer = HowLongMS
'get parameters
Me.Invoke(Sub()
tb = txtBox
curBack = tb.BackColor
curFore = tb.ForeColor
End Sub)
Do
'use new back color
Me.BeginInvoke(Sub()
tb.BackColor = newBack
tb.ForeColor = GetContrastColor(tb.BackColor)
End Sub)
Threading.Thread.Sleep(flshTM)
'then back to original
Me.BeginInvoke(Sub()
tb.BackColor = curBack
tb.ForeColor = curFore
End Sub)
Threading.Thread.Sleep(flshTM)
'continue until time expires
Loop While stpw.ElapsedMilliseconds < tmMS
End Sub)
t.Wait(20)
End Sub
Public Shared Function GetContrastColor(backColor As Color) As Color
'YIQ method to create a contrasting color
'http://24ways.org/2010/calculating-color-contrast/
Dim rv As Color
Dim R As Integer = backColor.R
Dim G As Integer = backColor.G
Dim B As Integer = backColor.B
Dim yiq As Integer = ((R * 299) + (G * 587) + (B * 114)) \ 1000
rv = If(yiq >= 128, Color.Black, Color.White)
Return rv
End Function
End Class
-
Feb 6th, 2018, 03:18 PM
#12
Thread Starter
Hyperactive Member
Re: Is it possible to make a textbox blinking with backColor?
This code is working fine but how to stop after 5 blinks?
Code:
Public Class Form1
Dim i As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
i += 1
If i = 1 Then
TextBox1.BackColor = Color.GreenYellow
ElseIf i = 2 Then
TextBox1.BackColor = Color.Yellow
i = 0
End If
End Sub
Last edited by VS2013; Feb 6th, 2018 at 03:22 PM.
-
Feb 6th, 2018, 03:45 PM
#13
Re: Is it possible to make a textbox blinking with backColor?
Step thru your code, you want to start a timer and when the timer ticks you want to toggle the TextBox control's BackColor up to 5 ticks. That would mean that you need to create a condition in your If/Then statement to check if your counter (i) equals 6 and if so then stop the timer. Rather than setting your counter back to 0 when it equals two, continue to increment it and use the MOD operator in place of the current conditional statements:
Code:
i += 1
If i = 6 Then
Timer1.Stop()
ElseIf i MOD 2 = 0 Then
TextBox1.BackColor = Color.GreenYellow
Else
TextBox1.BackColor = Color.Yellow
End If
Edit - Alternatively you could store the toggled color values into an Array and remove the conditional If/Then statement all together:
Code:
i += 1
Timer1.Enabled = (i < 6)
TextBox1.BackColor = {Color.GreenYellow, Color.Yellow}(i MOD 2)
Last edited by dday9; Feb 6th, 2018 at 03:55 PM.
Reason: wes4dbt post #14
-
Feb 6th, 2018, 03:51 PM
#14
Re: Is it possible to make a textbox blinking with backColor?
Shouldn't
Code:
Timer1.Enabled = (i = 6)
be
Code:
Timer1.Enabled = (i < 6)
-
Feb 6th, 2018, 03:55 PM
#15
Re: Is it possible to make a textbox blinking with backColor?
Thank you, I corrected it.
-
Feb 6th, 2018, 04:29 PM
#16
Thread Starter
Hyperactive Member
Re: Is it possible to make a textbox blinking with backColor?
Working quite nicely. One thing that I need to do here is ....
Call this event from frmInputSales by clicking on btnGetData button.
Code:
Private Sub btnGetData_Click(sender As Object, e As EventArgs) Handles btnGetData.Click
frmSales.txtModelName.Text = Me.txtModelName.Text
frmSales.txtMRP.Text = Me.txtMRP.Text
frmSales.txtQuantity.Focus()
'I need to call that function from this form (frmInputSales) so that the txtQuantity should start blinking on frmSales.
'Call frmSales.Timer1_Tick()
Me.Visible = False
End Sub
-
Feb 6th, 2018, 05:15 PM
#17
Re: Is it possible to make a textbox blinking with backColor?
I don't understand what it is that you're asking from post #16.
-
Feb 6th, 2018, 05:15 PM
#18
Re: Is it possible to make a textbox blinking with backColor?
You don't call the timer tick method. That is raised whenever the timer tick interval elapses. All you need to do is start the timer ticking. In your post #12, you enabled the timer in the Load event, which is what starts the flashing. You will need to set i back to 0, which you can do right after you stop the timer in the code that DDay provided. Alternatively, you can do it somewhere else, but that would be the most convenient place. Then, to get the control flashing again, you'd just enable the timer or call the .Start method of the timer.
Enabling the timer from btnGetData would be possible, the way you have it, so all you'd need to do is add:
frmSales.Timer1.Start()
You may want to do that as the very last line of the method, or it might be fine where you have the comment.
My usual boring signature: Nothing
-
Feb 7th, 2018, 08:25 AM
#19
Thread Starter
Hyperactive Member
Re: Is it possible to make a textbox blinking with backColor?
Code:
frmSales.Timer1.Start()
Thanks. It is working fine the first time I click on btnGetData button but when I click the second time, it just blink once.
-
Feb 7th, 2018, 10:42 AM
#20
Re: Is it possible to make a textbox blinking with backColor?
You will need to reset the counter (i) to 0 each time at the start.
-
Feb 7th, 2018, 12:28 PM
#21
Thread Starter
Hyperactive Member
Re: Is it possible to make a textbox blinking with backColor?
It's not working ... where shall I put this i = 0 line.
Code:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
i = 0
If btnSave.Enabled = True Then
i += 1
Timer1.Enabled = (i < 15)
txtModeOfTransport.BackColor = {Color.GreenYellow, Color.Yellow}(i Mod 2)
txtModelName.Focus()
End If
End Sub
-
Feb 7th, 2018, 12:49 PM
#22
Re: Is it possible to make a textbox blinking with backColor?
Not there.
Essentially, you want it right before you start the timer. However, i is a private member of the form, so you can't get to it directly. So, what you will need to do is to add a method to the form, and you might as well have it do all the work. Therefore, on frmSales, you could add something like:
Code:
Public Sub StartTheTimer()
i=0
Timer1.Start()
End Sub
Then call that method wherever you want to start the timer.
My usual boring signature: Nothing
-
Feb 7th, 2018, 02:28 PM
#23
Thread Starter
Hyperactive Member
Re: Is it possible to make a textbox blinking with backColor?
Thanks a lot for your kind support.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|