0

我一直在搞乱这里给出的答案 如何从 C# 中的另一个线程更新 GUI?

我收到错误 Method 'System.Windows.Forms.Label' Text not found from the following code

Private Delegate Sub setControlPropertyThreadSafeDelegate(control As Control, propertyName As String, propertyValue As Object)

Public Sub setControlPropertyThreadSafe(control As Control, propertyName As String, propertyValue As Object)
    If control.InvokeRequired = True Then
        control.Invoke(New setControlPropertyThreadSafeDelegate(AddressOf setControlPropertyThreadSafe), New Object() {control, propertyName, propertyValue})
    Else
        control.GetType().InvokeMember(propertyName, Reflection.BindingFlags.SetProperty, Nothing, control, New Object() {control, propertyName, propertyValue}) 'This is where the error occurs
    End If
End Sub

从一个单独的线程中,我使用以下行调用此方法

UI.setControlPropertyThreadSafe(UI.lblExcel, "Text", "Inserting Data into YTD Template")

任何有关此错误的帮助将不胜感激。

4

1 回答 1

3

线

control.GetType().InvokeMember(propertyName, Reflection.BindingFlags.SetProperty, _
    Nothing, control, New Object() {control, propertyName, propertyValue})

应该

control.GetType().InvokeMember(propertyName, Reflection.BindingFlags.SetProperty, _
    Nothing, control, New Object() {propertyValue})

最后一个参数InvokeMember是方法的参数列表,对于属性设置,它就是您希望设置的值。您链接到的问题是正确的。

于 2012-10-29T23:00:08.920 回答