2012-12-11 40 views
1

我有這樣的自定義類標籤文本不更新一個一個的Visual Basic

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 

我有我放在這個labelScroll上還放置一個按鈕形式:我對按鈕的單擊事件的代碼:

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 

當我點擊按鈕時會發生什麼,它需要2秒鐘,然後在三行上顯示「1」「2」「3」。實際上應該發生的是,當用戶單擊該按鈕時,會出現「1」,然後執行Threading.Thread.Sleep(1000),以便程序等待1秒鐘,然後在下一行打印「2」。

爲什麼不會發生這種情況?

回答

4

在標籤上設置文本將使控件失效 - 意味着下次事件隊列處理(有效)時它將重新繪製。當你在UI線程上睡覺時,這不會發生 - 嘗試在MyBase.Text = ...行後立即添加MyBase.Update()以強制立即更新。

+0

非常感謝!它現在的作品:) – DemCodeLines

+0

沒問題 - 很高興有幫助! –

相關問題