Results 1 to 23 of 23

Thread: [RESOLVED] Is it possible to make a textbox blinking with backColor?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    326

    Resolved [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?

  2. #2
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Re: Is it possible to make a textbox blinking with backColor?

    Just change the backcolor with a timer.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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:
    1. TextBox1.BackColor = If(TextBox1.BackColor = Color.Red, Color.Yellow, Color.Red)

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Is it possible to make a textbox blinking with backColor?

    Quote Originally Posted by Shaggy Hiker View Post
    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?

  6. #6
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    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.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: Is it possible to make a textbox blinking with backColor?

    Quote Originally Posted by jmcilhinney View Post
    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

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    326

    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?

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    326

    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.

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    326

    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.

  13. #13
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  14. #14
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,196

    Re: Is it possible to make a textbox blinking with backColor?

    Shouldn't
    Code:
    Timer1.Enabled = (i = 6)
    be

    Code:
    Timer1.Enabled = (i < 6)

  15. #15
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Is it possible to make a textbox blinking with backColor?

    Thank you, I corrected it.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    326

    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

  17. #17
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  18. #18
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    326

    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.

  20. #20
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    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.

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    326

    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

  22. #22
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    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

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    326

    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
  •  



Click Here to Expand Forum to Full Width