DemCodeLines
DemCodeLines

Reputation: 1920

Label text not updating one by one Visual Basic

I have this custom class

Public Class labelScroll
    Inherits Label

    Public Shadows Property Text As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            Dim add As String = ""
            Dim result As String()
            Dim i As Integer
            result = Split(value, vbLf)
            Dim n As Integer = 30
            If (result.Length < n) Then
                n = result.Length
            End If
            Dim start As Integer = result.Length - n
            For i = start To result.Length - 1 Step 1
                add += result(i) + Environment.NewLine
            Next
            MyBase.Text = add
        End Set
    End Property
End Class

I have a form that I placed this labelScroll on and also placed a button: I have this code for the button's click event:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    LabelScroll1.Text = "1"
    Threading.Thread.Sleep(1000)
    LabelScroll1.Text += "2"
    Threading.Thread.Sleep(1000)
    LabelScroll1.Text += "3"
End Sub

What happens when I click the button is that it takes 2 seconds and then just shows "1" "2" "3" on three lines. What actually should happen is that when the user clicks the button, "1" appears and then Threading.Thread.Sleep(1000) is executed so the program waits for 1 second then prints "2" on the next line.

Why isn't this happening?

Upvotes: 2

Views: 7573

Answers (1)

Will A
Will A

Reputation: 24988

Setting the text on a label invalidates the control - meaning it will redraw the next time the event queue is processed (effectively). This won't happen whilst you're sleeping on the UI thread - try adding MyBase.Update() immediately after the MyBase.Text = ... line to force an immediate update.

Upvotes: 5

Related Questions