且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用名称作为字符串调用单独的 Windows 窗体

更新时间:2023-11-05 11:42:52

试试这个,你必须导入System.Windows.FormsSystem.Reflection

Try this, you have to import System.Windows.Forms and System.Reflection

首先将表单名称放入strCreatedFromButton然后找到它.

First get the form name into the strCreatedFromButton then find it.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim strCreatedFromButton As String = "Form3"

    Dim frm As New Form
    frm = DirectCast(CreateObjectInstance(strCreatedFromButton), Form)
    frm.Show()
End Sub

Public Function CreateObjectInstance(ByVal objectName As String) As Object
    Dim obj As Object
    Try
        If objectName.LastIndexOf(".") = -1 Then
            objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
        End If

        obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)

    Catch ex As Exception
        obj = Nothing
    End Try
    Return obj

End Function