How to create a program pause to update a label

MikeCombe
MikeCombe used Ask the Experts™
on
I want to display something in a label control, that updates when the program changes. Example, I want to display the "fruit of the day"....where it changes every 10 seconds. I put a "program pause' loop for 10 seconds in the following code. However, I only get to see the LAST item. I want to see each item, wait 10 seconds and see the next item. What am I doing wrong ???

    Public Sub Fruit_of_the_Day()
        Dim curFruit As String = ""
        Dim j_Fruit As Integer
        Dim k_Fruit As Integer

        For j_Fruit = 1 To 3
            If j_Fruit = 1 Then
                curFruit = "apple"
            ElseIf j_Fruit = 2 Then
                curFruit = "orange"
            ElseIf j_Fruit = 3 Then
                curFruit = "banana"
            End If

            'MsgBox("The fruit of the day is: " & curFruit)
            lbl_FruitName.Text = "The fruit of the day is: " & curFruit

            For k_Fruit = 1 To 10000
                ' do nothing - program pause
            Next
        Next
    End Sub
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009
Commented:
You can do something like this:
Public Sub Fruit_of_the_Day()
        ...
            lbl_FruitName.Text = "The fruit of the day is: " & curFruit
            Pause(10)
        Next
    End Sub

    Private Sub Pause(ByVal delayInSeconds As Integer)
        Dim dt As DateTime = DateTime.Now.AddSeconds(delayInSeconds)
        While dt.Subtract(DateTime.Now).TotalMilliseconds > 0
            System.Threading.Thread.Sleep(50)
            Application.DoEvents()
        End While
    End Sub

Open in new window

Author

Commented:
Perfect !!!

Thank you very much.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
Top Expert 2009

Commented:
Glad that worked.  Just be aware that it may not be the "best" approach.  You could also use a Timer control and change the Label in the Tick() event.

Author

Commented:
actually I tried the timer control previously. Your solution is a great fit for my application. Once again, thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial