VS 2008 Custom dialog in a class library-VBForums
Results 1 to 10 of 10

Thread: Custom dialog in a class library

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Question Custom dialog in a class library

    Hello.

    It's been a while since I last posted something, but this attracted my attention. Since many of my programs share the same type of code, I decided to dump it all in a class library for other programs to use.

    Now I have a problem, Windows Forms in a class library can't be displayed. There is no "Show(Dialog)" command, only some for event handling. All properties are gone as well. The only way I managed to display a form is by making it as a new variable: 'Dim f as windows.forms.form

    But this won't work because of the enormous amount of handlers added to the original.

    How can I show my (TextureBrowse in my case) Dialog so other programs can use it?

    Help greatly appreciated.

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Custom dialog in a class library

    1. Create a form
    2. Include the form class into your class library's namespace.
    3. Expose a publis method that shows your form:

    Code:
    Class MyClassLib
    
         Class MyCustomForm
             Inherits Windows.Forms.Form
             ' Your corm logic here
             ' Don't forget to move all your designer code here as well.
         End Class
         
         Public Function ShowDialog() As DialogResult
             Dim f As New MyCustomForm
             Return f.ShowDialog
         End Function
    End Class

  3. #3
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Custom dialog in a class library

    The only difference between forms in a class library and forms in a Windows Forms Application is the creation of default instances.

    In a Windows Forms app, whenever you create a form, a default instance, which is basically a variable (of the same name as your form), is created too, which you can use:
    Code:
    Form1.Show()
    In this code snippet, Form1 does not refer to the class Form1 at all. It refers to an instance of the Form1 class: the default instance.

    Somewhere, in a hidden place (I don't even know where), there should be some kind of declaration that also does
    Code:
    Form1 = New Form1()

    In a Class Library, no default instance is created, so you have to create your own. What you were trying was almost correct, but instead of creating a new instance of the Form class, you create a new instance of the Form1 class:
    Code:
    Dim f As New Form1
    Now, use f instead of the name Form1.
    Code:
    f.Show()

    The code cicatrix showed would work too but can be very dangerous if you don't know what you are doing. That code returns a new instance every time the ShowDialog method is called. That may become very confusing. If you call ShowDialog once and have the user edit something on the form, then close it, then use ShowDialog again, the data will be gone because the second time, a different instance of your form is shown.

  4. #4
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    24,046

    Re: Custom dialog in a class library

    Love those default instances....oh wait, no I don't. They cause more trouble than they could ever be worth.
    My usual boring signature: Nothing

  5. #5
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Custom dialog in a class library

    Quote Originally Posted by NickThissen View Post
    The code cicatrix showed would work too but can be very dangerous if you don't know what you are doing. That code returns a new instance every time the ShowDialog method is called. That may become very confusing. If you call ShowDialog once and have the user edit something on the form, then close it, then use ShowDialog again, the data will be gone because the second time, a different instance of your form is shown.
    Well this is intentional. A dialog window is supposed to appear on the screen, let user make some choices and either *CONFIRM* them or *CANCEL* them by clicking on the relevant buttons. Then the dialog closes and returns its result to an owner. Whichever properties or methods the form exposes is up to a developer to decide. Re-using of the old instance can be as much confusing as using the new one each time. In fact, it can be even more confusing. If a user has confirmed something on a form the state should be saved elsewhere. A dialog is just a dialog - a front-end interface, nothing more.
    OpenFile and SaveFile dialogs are perfect examples to my words.

  6. #6

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Custom dialog in a class library

    And can you actually use a windows form in the solution list?

    And when using the methode mentioned by cicatrix, can I just add event handlers and others in the MyCustomForm class?

    Thanks for the help so far

    EDIT:

    I tried using:
    Code:
    Class MyClassLib
    
         Class MyCustomForm
             Inherits Windows.Forms.Form
             ' Your corm logic here
             ' Don't forget to move all your designer code here as well.
         End Class
         
         Public Function ShowDialog() As DialogResult
             Dim f As New MyCustomForm
             Return f.ShowDialog
         End Function
    End Class
    And exported it as a library dll.
    I made a reference to it in my test application, but now it can't find the subs and functions.
    Here is my code:

    Code:
    Public Class TextureBrowseDialog
        Public Function ShowDialog() As Windows.Forms.DialogResult
            Dim f As New Form1
            Return f.ShowDialog
        End Function
    
        Public Function GetTexturePath() As String
            Return TextureBrowserSelTexture
        End Function
        Public Function GetTextureImage() As Drawing.Image
            Return TextureBrowserImageTexture
        End Function
        Public Sub SetRoot(ByVal RootPath As String)
            TextureBrowserRoot = RootPath
        End Sub
        Public Sub SetTexturePath(ByVal SelectedPath As String)
            'TODO
        End Sub
    
    End Class
    Anyone knows why it can't find the public subs in this class?
    (it did with a previous library...)
    Last edited by bergerkiller; Mar 3rd, 2010 at 12:38 PM.

  8. #8
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Custom dialog in a class library

    I don't know why you're trying to do it so difficult. Forms work in exactly the same way in a Windows Forms app and in a Class Library. The only difference is that, in a Class Library, you need to explicitly create a new instance of the form before you can show it. You even need that instance in a Windows Forms app too, but you don't need to do it yourself, because VB will create one for you (the default instance, named the same as the form).

    So, whereever you are using stuff like
    Code:
    TextureBrowseDialog.Show()
    You need to replace it with
    Code:
    Dim f As New TextureBrowseDialog
    f.Show()
    That's it. Nothing else needs to change. The code in the TextureBrowseDialog is the same as if it was in a Windows Forms app.

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Custom dialog in a class library

    Well atm I was doing it like this:

    I added a form1 item to the solution list.
    I added a module to store important variables and fucntions used inside.
    In the main class TextureBrowseDialog I added in the showdialog the code to make the new isntance and show it.

    This all worked, but did not display.

    Now sorted that out, forgot to add "shared", now it does display.
    Testing it now...Thanks for the help so far

    EDIT:

    Wohoo it worked.
    For the people experiencing the same problem:
    Simply add a widnows form to the project, and add a shared sub/function similar to this:
    Code:
        Public Shared Function ShowDialog() As Windows.Forms.DialogResult
            Dim f As New Form1
            Return f.ShowDialog
        End Function
    Seems you need to use a shared sub/function, else it will not display it.

    Thanks everyone for the great help
    Last edited by bergerkiller; Mar 3rd, 2010 at 01:08 PM.

  10. #10
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Custom dialog in a class library

    I think you need to do some reading on classes and instances of classes. In particular, the difference between an instance method and a shared method. Then you may also understand what a default form instance is and how to use forms without them.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.