Access to controls between forms

I have a thread running in a child form, I want to activate the control in the parent form, but I cannot. It works great if it is made from a child form that creates a user interface stream:

(FormMain.SetControlPropertyValue (FormMain.RBSQL2005, Validated, True))

but not from a thread running in a 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

What am I doing wrong?

alt text

0
source share
2 answers
 FormMain.SetControlPropertyValue(FormMain.RBSQL2005, "Checked", True)

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

, , "FormMain" . , <ThreadStatic>. , , , . , Show() . , - . Show() , . , . , , , InvokeRequired False, , .

FormMain. , Me. , . Q & D Application.OpenForms(0)

+3

Source: https://habr.com/ru/post/1770576/


All Articles