通过 MainControl 动态创建的 UserControl 访问子控件或值

问题描述 投票:0回答:1

你好,我有一个这样的表格:

--------------------------------
|BTN|BTN|BTN|                   |    
--------------------------------
|                              |
|            tabControl        |
|                              |
--------------------------------

因为我有数百个用户控件,所以我尝试通过此过程以优雅的方式打开它们:

    Private Function OpenProg(ByVal prognr As Integer) As Boolean
        Dim prow = (From p In ds.tblPrograms Where p.ProgramID = prognr).ToList
        Dim found As Boolean = True

        If prow.Count > 0 Then
            Dim a As Assembly = Assembly.GetEntryAssembly
            Dim t As System.Type = a.[GetType]("<SoftwareName>.usc" & prow(0).ProgramID)
            If t IsNot Nothing Then
                Dim o As Object = System.Activator.CreateInstance(t, True)
                Dim usc As System.Windows.Forms.UserControl = DirectCast(o, System.Windows.Forms.UserControl)
                Dim tabPage As New XtraTabPage
                tabPage.Text = prow(0).ProgramName
                tabPage.Tag = prow(0).ProgramID
                tabCtrl.TabPages.Add(tabPage)
                tabPage.Controls.Add(usc)
                usc.Dock = DockStyle.Fill
                tabCtrl.SelectedTabPage = tabPage
            End If
        Else
            found = False
        End If
        Return found
    End Function

这样就创建了一个具有相应 usercontrolID 和选项卡的新用户控件。

但我不知道如何访问此用户控件,例如按下按钮后。这就是我为保存按钮写的:

    Private Sub bbiSave_ItemClick(sender As Object, e As ItemClickEventArgs) Handles bbiSave.ItemClick
        Dim a As Assembly = Assembly.GetEntryAssembly
        Dim t As System.Type = a.[GetType]("<SoftwareName>.usc" & CInt(e.Item.Tag))
        For Each ctr As System.Windows.Forms.Control In tabCtrl.SelectedTabPage.Controls
            If ctr.Name = "usc" & CInt(e.Item.Tag) Then

                Dim usc As System.Windows.Forms.UserControl = TryCast(ctr, t) '<- incorrect, t is not defined
            End If
        Next



    End Sub

但我想访问 Save() 或文本字段等:

Partial Public Class usc10
    ...

    Public Sub Save()
        ... something
    End Sub
End Class

当然,我可以使用类型化的类型来运行它。 (Dim usc As New usc10; usc.Save),但我不知道如何用动态类型来归档它。

vb.net types dynamic user-controls
1个回答
0
投票

我将我的问题复制并粘贴到 ChatGPT,他给了我答案,疯狂......:-O 及其工作原理:

Private Sub bbiSave_ItemClick(sender As Object, e As ItemClickEventArgs) Handles bbiSave.ItemClick
    Dim a As Assembly = Assembly.GetEntryAssembly
    Dim t As System.Type = a.[GetType]("OptiMind_Fusion.usc" & CInt(e.Item.Tag))
    For Each ctr As UserControl In tabCtrl.SelectedTabPage.Controls
        If ctr.Name = "usc" & CInt(e.Item.Tag) Then
            Dim uscType As Type = ctr.GetType() ' Get the actual type of the UserControl
            Dim saveMethod As MethodInfo = uscType.GetMethod("Save") ' Get the Save method

            If saveMethod IsNot Nothing Then
                ' Create an instance of the UserControl
                Dim uscInstance As Object = Activator.CreateInstance(uscType)

                ' Invoke the Save method on the instance
                saveMethod.Invoke(uscInstance, Nothing)
            End If

        End If
    Next
End Sub

但是有点慢,有什么优化吗?

© www.soinside.com 2019 - 2024. All rights reserved.