Feedback

Eigene Form aus ClassLibrary DLL Aufrufen

Sprache: VB

Diese Fuktion ermöglicht eine Form aus eigene ClassLibrary DLL aufzurufen. Einfach den Pfad der DLL angeben und Name der Form die im DLL ist angeben. [u]Folgende Importe werden benötigt:[/u] [b]Imports System.Reflection Imports System.IO[/b] [b][u]Aufruf Beispiel:[/u][/b] [b][u]Form1 aus ClassLibrary1.dll aufrufen[/u][/b] CallDLLForm(Application.StartupPath & "ClassLibrary1.dll", "Form1") [b][u]Form1 aus ClassLibrary2.dll aufrufen[/u][/b] CallDLLForm(Application.StartupPath & "ClassLibrary2.dll", "Form1") ———————————————————————————————— [b][u] 2 Formen aus einer ClassLibrary1.dll aufrufen Form1 aus ClassLibrary1.dll aufrufen[/u][/b] CallDLLForm("C:ClassLibrary1.dll", "Form1") [b][u]Form2 aus ClassLibrary1.dll aufrufen[/u][/b] CallDLLForm("C:ClassLibrary1.dll", "Form2")
    ''' <summary>Ruft eine Form1 auf in einer DLL</summary>
    ''' <param name="DLLpath">Pfad zur DLL (Beispiel: C:TEST.dll)</param>
    ''' <param name="FormName">Form Name in DLL (Beispiel: Form1 oder frmMain)</param>
    Public Sub CallDLLForm(ByVal DLLpath As String, ByVal FormName As String)
        Dim DLLFilenameEx As String = Path.GetFileName(DLLpath)
        Dim Filename As String = Path.GetFileNameWithoutExtension(DLLpath)
        Try
            Dim A As [Assembly] = [Assembly].LoadFrom(DLLpath)
            Dim B As [Module] = A.GetModule(DLLFilenameEx)
            Dim C As Type = B.GetType(Filename & "." & FormName)
            Dim D As Form = DirectCast(Activator.CreateInstance(C), Form)
            D.ShowDialog()
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    ''' <summary>Ruft eine Form1 auf in einer DLL</summary>
    ''' <param name="DLLpath">Pfad zur DLL (Beispiel: C:TEST.dll)</param>
    ''' <param name="FormName">Form Name in DLL (Beispiel: Form1 oder frmMain)</param>
    Public Sub CallDLLForm(ByVal DLLpath As String, ByVal FormName As String)
        Dim DLLFilenameEx As String = Path.GetFileName(DLLpath)
        Dim Filename As String = Path.GetFileNameWithoutExtension(DLLpath)
        Try
            Dim A As [Assembly] = [Assembly].LoadFrom(DLLpath)
            Dim B As [Module] = A.GetModule(DLLFilenameEx)
            Dim C As Type = B.GetType(Filename & "." & FormName)
            Dim D As Form = DirectCast(Activator.CreateInstance(C), Form)
            D.ShowDialog()
        Catch ex As Exception
            MessageBox.Show(ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

2 Kommentare

  1. Warum so umständlich?
    1. Referenz auf die Assembly wo die Form ist.
    2. In deinem Code bei den Usings einfach „using NamespaceDeinerAssembly;“
    3. In der gewünschten stelle „FormName frm = new FormName();“ und anschließend frm.ShowDialog() oder „.Show();

    Lg Fawk_18