Form einer ClassLibrary DLL aufrufen

    • VB.NET

      Form einer ClassLibrary DLL aufrufen

      Ich habe eine Funktion geschrieben wo man eine Form aus der ClassLibrary aufrufen kann.
      In der Funktion Pfad zur DLL angeben und Name der Form die in der ClassLibrary DLL ist.

      Folgende Importe werden benötigt:

      VB.NET-Quellcode

      1. Imports System.Reflection
      2. Imports System.IO



      Die Funktion:

      VB.NET-Quellcode

      1. ''' <summary>Ruft eine Form auf in einer DLL</summary>
      2. ''' <param name="DLLpath">Pfad zur DLL (Beispiel: C:\TEST.dll)</param>
      3. ''' <param name="FormName">Form Name in DLL (Beispiel: Form1 oder frmMain)</param>
      4. Public Sub CallDLLForm(ByVal DLLpath As String, ByVal FormName As String)
      5. Dim DLLFilenameEx As String = Path.GetFileName(DLLpath)
      6. Dim Filename As String = Path.GetFileNameWithoutExtension(DLLpath)
      7. Try
      8. Dim A As [Assembly] = [Assembly].LoadFrom(DLLpath)
      9. Dim B As [Module] = A.GetModule(DLLFilenameEx)
      10. Dim C As Type = B.GetType(Filename & "." & FormName)
      11. Dim D As Form = DirectCast(Activator.CreateInstance(C), Form)
      12. D.ShowDialog()
      13. Catch ex As Exception
      14. MessageBox.Show(ex.Message.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
      15. End Try
      16. End Sub



      Beispiel Aufruf einer Form in ClassLibrary DLL:

      VB.NET-Quellcode

      1. 'Form1 aus ClassLibrary1.dll aufrufen
      2. CallDLLForm(Application.StartupPath & "\ClassLibrary1.dll", "Form1")
      3. 'Form1 aus ClassLibrary2.dll aufrufen
      4. CallDLLForm(Application.StartupPath & "\ClassLibrary2.dll", "Form1")
      5. 'Form1 aus ClassLibrary1.dll aufrufen
      6. CallDLLForm("C:\ClassLibrary1.dll", "Form1")
      7. 'Form2 aus ClassLibrary1.dll aufrufen
      8. CallDLLForm("C:\ClassLibrary1.dll", "Form2")



      Viel Spaß :D