Storing a form in a dll

In my project I have four forms. Is there a way to store a form in a dll file instead of storing it in the exe file. I want to be able to call the form to show just like I would normally. If there are any links on how to do this in vb.net that would be great also. Thanks.



Answer this question

Storing a form in a dll

  • a.i.

    Is there a way I could add a reference through code. So, when my form loads it can scan it's directory and if a dll exists then it will add an item to it's menustrip. It can then also add a reference.

    Or is that just too complicated to be feasible

    Thanks for the quick response.



  • George.Saliba

    Start a new project as an ActiveX DLL and put the forms into it. Compile it, then create a new project and include the DLL you used as a reference. You can then create a new object to represent the DLL, and call the forms inside it by using the .Show() method.

    Hope you find this useful!



  • Eric23

    Ok, I did the Add Reference... suggestion. Now I have another problem.

    I Use the following code to make the form open in a tab (this code is part of Form1.vb, not part of the dll).

    Private Function newTab() As Boolean
    Try
    TabControl1.SelectTab(newPage)
    Catch ex As Exception
    newForm.TopLevel = False
    newForm.Dock = DockStyle.Fill
    newForm.Visible = True
    newPage.Controls.Add(newForm)
    TabControl1.TabPages.Add(newPage)
    End Try
    End Function

    Private Sub menu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles menu.Click

    newForm = New Class1.Class1 : newPage.Text = "Class1" : newTab()
    End Sub

    Now the form that opens has a button to close the tab and remove it from the Tab Control. I used the following code when the form was still part of my project.

    Form1.TabControl1.TabPages.Remove(Form1.TabControl1.SelectedTab)

    Now, since I cannot reference Form1 (since the form is now part of an external dll) I use the following.

    Me.Parent.Controls.Clear()

    This removes the form from the tab but the tab still remains. There is no way (that I can find) to remove the tab from the Tab Control (since I cannot reference Form1, I cannot reference TabControl1 on it).

    This is the only problem I am having right now. Thank you for any help you can provide.


  • Michal Levy

    The only option I can choose it the Class Project. Is this what you meant I don't have a template for ActiveX DLL.

    Also, I used 'Add Existing Item..' and added my form Form2.vb to the project. Now I have 102 errors. Some errors are like:
    Type 'System.Windows.Forms.Form' is not defined.
    Type 'System.Windows.Forms.ToolStrip' is not defined
    and so on for all of my controls.

    I obviously did something wrong. Any idea why it is doing this Thank you for your response.


  • Vahid66

    Hmm that was too simple. Maybe that is why I never thought of that. I'll give it a try. Thanks.


  • Superboy

    reference it by going to project menu and selecting "add reference" and then select your dll. You could also create a project level reference by including the dll project in your exe solution

    HTH



  • Sreekanth Ammisetty

    Here's two similar ways of doing it:

    Capture the Click event of the button in the tab page, and handle the tab removal in it.

    Or a little bit more advanced, create a delegate variable for the form that's going to be hosted in the tab page, point it to a method for handling the tab removal.



  • TomPepe

    Ok I got it to compile.

    Now I have another question. I want it to be a standalone dll. It will be stored in the same directory as my exe file. How would I reference it. I tried the normal methods but since in is not part of my solution it gives errors.

    Thanks.



  • arvindbksc

    Thanks Ryan.

    And don't worry about it robinjam. Nobody is perfect.



  • Matanel Sindilevich

    I find it easier to create a windows forms application to develop the forms. When the project is complete I open the project properties and change the project type to class library and rebuild the solution to create my dll.

  • question mark

    Yea you chose the correct one, a Class Library project compiles a dll instead of an exe. If you are using Windows Forms then you need to add a reference to System.Windows.Forms.dll, which isn't added for you automatically(unlike Windows Application Project) when creating a Class Library Project.



  • nathan koterba

    Sorry about that... I was thinking how to do it in Vb6 instead of VB.Net...

  • joseadolfo

    Can you explain more on the second method you mentioned. I cannot do the first method because the button is in the form that is stored in the dll.


  • Vonfranzken

    If someone was willing to look at my project then I could send it to them via email. It might be easier to figure out a solution. Thanks.


  • Storing a form in a dll