开发者

Accessing controls between forms

I have a thread running on a child form, I want to activate a control on the parent form but cannot. It works fine if It's done from the child forms UI thread:

(FormMain.SetControlPropertyValue(FormMain.RBSQL2005, "Checked", True))

but not from a thread running on the child form:

Public Class FormRestoreDB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim t = New Thread(AddressOf UpdateListView1)
    t.Start()
End Sub


Private Sub UpdateListView1()
    'FormMain.SetControlPropertyValue(FormMain.RBSQL2005, "Checked", True)
    FormMain.RBSQL2005.Checked = True
End Sub

End Class


Public Class FormMain

Delegate Sub SetControlValueCallback(ByVal oControl As Control, ByVal propName As String, ByVal propValue As Object)

Public Sub SetControlPropertyValue(ByVal oControl As Control, ByVal propName As String, ByVal propValue As Object)
    If (oControl.InvokeRequired) Then

        Dim d As New SetControlValueCallback(AddressOf SetControlPropertyValue)
        oControl.Invoke(d, New Object() {oControl, propName, propValue})
    Else
        Dim t As Type = oControl.[GetType]()
        Dim props As PropertyInfo() = t.GetProperties()
        For Each p As PropertyInfo In props
            If p.Name.ToUpper() = propName.ToUpper() Then
                p.SetValue(oControl, propValue, Nothing)
            End If
        Next
    End If
End Sub

End Class

Wha开发者_开发知识库t am I doing wrong?

Accessing controls between forms


 FormMain.SetControlPropertyValue(FormMain.RBSQL2005, "Checked", True)

This is one of the horrors induced by VB.NET's support for using the type name of a form to refer to the instance of the form. It is an anachronism carried over from VB6 where using the type name of a form was normal.

This falls apart when you use threads because "FormMain" has thread affinity. In other words, it has the <ThreadStatic> attribute. When you use it in a worker thread then you get a new instance of the form, not the one that the user is looking at. This new instance isn't visible because its Show() method was never called. Which is why you don't see your code having any effect. If you call Show() in your code then you'll see it pop up. It is however quite dead, the thread isn't pumping a message loop. Another effect you should have noticed was that InvokeRequired is False, you know that can't be right.

You will have to stop using FormMain. If this code lives inside that class then you can simply use Me. If it doesn't then you'll have to pass the form reference with a property or a constructor argument. The Q&D fix is to use Application.OpenForms(0)


You need to use Invoke (http://msdn.microsoft.com/en-us/library/aa334883%28v=VS.71%29.aspx) or BeginInvoke to interact with the parent thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜