2011年6月22日 星期三

vb.net 動態載入 UserControl

 Private Function LoadUserControl(ByVal FormName As String) As UserControl
        Dim ProjectName As String =
        Reflection.Assembly.GetExecutingAssembly.GetName.Name
        Try
            Dim tyOfStringVariable As Type = Type.GetType(ProjectName & "." &
            FormName)
            Dim ucObject As Object = Activator.CreateInstance(tyOfStringVariable)
            'DirectCast(ucObject, UserControl).StartPosition = FormStartPosition.CenterParent
            'DirectCast(ucObject, Form).ShowDialog()
            Return DirectCast(ucObject, UserControl)
        Catch ex As Exception
            ' TODO
            Throw New Exception("UserControl Not Exist!")
        End Try
        Return Nothing
    End Function

沒有留言:

張貼留言