我正在開發一個VB.Net項目,並試圖從正在後臺運行的線程(必要的惡意)向我的表單添加標籤。從線程向表單添加標籤
我已經創建了全局變量SmallTextLabel(類型:標籤),並已分配了所有我想要的屬性。我現在只是試圖將它添加到我的表單使用Me.Controls.Add(SmallTextLabel)。每次運行此代碼時,都會告訴我必須調用該控件。我在屏幕上尖叫,那正是我想要做的,但它聽着嗎?!
無論如何,我已經搜索了過去4個小時的互聯網,並沒有設法解決這個問題,所以請...任何人都可以在這裏爲我閃耀一些光芒?
這是我目前正在AddSmallLabelControl(SmallTextLabel)調用代碼
修訂例如
Private Delegate Sub AddSmallLabelControlHandler(ByVal test As Label)
Private Sub AddSmallLabelControl(ByVal test As Label)
If Me.InvokeRequired Then
Dim d As New AddSmallLabelControlHandler(AddressOf AddSmallLabelControl)
Me.Invoke(d, test)
Else
Me.Controls.Add(test)
test.BringToFront()
End If
End Sub
Private Sub ShowScanConfirmationLabel()
Dim SmallTextLabel As New Label
Dim TahomaSmall As New Font("Tahoma", 20, FontStyle.Bold)
With SmallTextLabel
.Height = 40
.Width = 312
.Location = New Point(3, 121)
.BackColor = Color.Green
.Text = "Testing"
.TextAlign = ContentAlignment.TopCenter
.Font = TahomaSmall
End With
End With
Call AddSmallLabelControl(SmallTextLabel)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MakeLabelsVisibleHandler As New ThreadStart(AddressOf MakeLabelsVisible)
Dim timerThread As New Thread(MakeLabelsVisibleHandler)
timerThread.Start()
End Sub
Private Sub MakeLabelsVisible()
ShowScanConfirmationLabel()
End Sub
堅持。你在什麼線程初始化標籤? (你在哪裏設置'SmallTextLabel = New Label()'),如果你檢查'Me.SmallTextLabel.InvokeRequired',那麼你應該調用'SmallTextlabel.Invoke(...)',但是當你定義並初始化laben單獨的線程,你可能想檢查'Me.InvokeRequired'而不是... – MrPaulch
我目前正在初始化標籤在調用thread.Dim SmallTextLabel作爲新標籤 –
如果不是Me.SmallTextLabel.InvokeRequired然後拋出新的異常(「你是做錯了,試着用我來代替「) –