Blink TextBox on UserForm

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Blink TextBox on UserForm

Post by YasserKhalil »

Hello everyone

I am trying the following code that blinks the textbox4 if it equals to date

Code: Select all

Private Sub TextBox4_AfterUpdate()
    Static b As Boolean
    Static i As Integer

    With TextBox4
        If Not IsDate(.Text) Then Exit Sub

        If CDate(.Text) = Date Then
            Do Until i >= 5
                If b Then .BackColor = vbYellow Else .BackColor = vbGreen
                Application.Wait Now + TimeValue("00:00:01")
                b = Not b
                i = i + 1
                Debug.Print i
            Loop
    
            b = False: i = 0
        End If
    End With
End Sub
I put the variable i to 5 so I expect to see the blinking 5 times but I just noticed it twice not more. Any idea how this can be fixed?

User avatar
HansV
Administrator
Posts: 78483
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Blink TextBox on UserForm

Post by HansV »

Insert the following line above the Application.Wait line:

Code: Select all

                Me.Repaint
This forces the userform to be updated.

Warning: in general, making interface elements blink is not recommended; it might irritate users or even have an adverse effect.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Blink TextBox on UserForm

Post by YasserKhalil »

Thanks a lot for the advice. I tested using RePaint but I got just blinking three times instead of five times.

User avatar
HansV
Administrator
Posts: 78483
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Blink TextBox on UserForm

Post by HansV »

When I enter today's date, the text box becomes green, yellow, green, yellow and finally green.
Best wishes,
Hans

User avatar
HansV
Administrator
Posts: 78483
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Blink TextBox on UserForm

Post by HansV »

Here is a low-quality video (zipped) of what I see:
Clip.zip
You do not have the required permissions to view the files attached to this post.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Blink TextBox on UserForm

Post by YasserKhalil »

Yes that's what I am talking about. The variable i equals to 5 so I expect 5 times or I think I missed it must be 10 so as to get this blinking 5 times.
Thanks a lot Mr. Hans for great help.

User avatar
HansV
Administrator
Posts: 78483
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Blink TextBox on UserForm

Post by HansV »

Do Until i >= 5 will get you 5 color changes. If you want 5 times green AND yellow, you must indeed use Do Until i >= 10.
Best wishes,
Hans

YasserKhalil
PlatinumLounger
Posts: 4913
Joined: 31 Aug 2016, 09:02

Re: Blink TextBox on UserForm

Post by YasserKhalil »

Yes I recognized that when I posted the last reply
Thank you very much for your helpful and great support