To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Article :: Building Dynamic Systems with Expressions in .NET
How Is XML Like An Interface?
Understanding Covariance and Contravariance
Print VS 2010 Keyboard Shortcut References in Letter (8.5x11in) and A4 (210×297mm) Sizes
Updated Productivity Power Tools



Go Back   VBForums > Visual Basic > Visual Basic .NET

Reply Post New Thread
 
Thread Tools Display Modes
Old Mar 3rd, 2010, 07:22 AM   #1
bergerkiller
Member
 
Join Date: Jul 09
Posts: 51
bergerkiller is an unknown quantity at this point (<10)
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.
bergerkiller is offline   Reply With Quote
Old Mar 3rd, 2010, 07:46 AM   #2
cicatrix
PowerPoster
 
cicatrix's Avatar
 
Join Date: Dec 09
Location: Moscow, Russia
Posts: 2,237
cicatrix is a glorious beacon of light (400+)cicatrix is a glorious beacon of light (400+)cicatrix is a glorious beacon of light (400+)cicatrix is a glorious beacon of light (400+)cicatrix is a glorious beacon of light (400+)
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
__________________
#define true ((rand() % 2)? true: false) // Debug THAT!

cicatrix is offline   Reply With Quote
Old Mar 3rd, 2010, 08:39 AM   #3
NickThissen
PowerPoster
 
Join Date: Apr 07
Location: The Netherlands
Posts: 4,053
NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)
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.
__________________
Controls:
*NEW* Double TrackBar - Editable ListBox - Outlook Navigation Bar - ColorListBox with images - Advanced ToolStripContainer - RadioButtonGroup Control - Expandable Groupbox - Wizard Template Usercontrol (full Design-time support!) ... Now with Aero Glass support! - 3D Separator - ListView Options Screen - TabControl with tab-specific ContextMenuStrips and Tab-Dragging

Menustrip and Toolstrip Renderers:
Visual Studio 2010 - Customizable Menu/ToolStrip (incl Office 2007 + all Office 2003 styles) - Office 2007 - Visual Studio 2008 - [WIP]Vista Toolstrip

Misc:
*NEW* Snapping windows - *NEW* Advanced Shape Editor like Visual Studio - ByVal vs ByRef and value types vs reference types - Game Of Life - OOP Tic Tac Toe game example - Moving and Resizing a Control or a Borderless Form, using SendMessage (smooth) - Manual 'MDI Window List' menu - Tabbed MDI Text Editor - Very Extensive MDI text editor - Real Synchronized RichTextBox scrolling - Notepad-like New/Open/Save/SaveAs/Exit behaviour
NickThissen is offline   Reply With Quote
Old Mar 3rd, 2010, 09:30 AM   #4
Shaggy Hiker
Loquacious User
 
Shaggy Hiker's Avatar
 
Join Date: Aug 02
Location: Idaho
Posts: 12,058
Shaggy Hiker is a name known to all (1000+)Shaggy Hiker is a name known to all (1000+)Shaggy Hiker is a name known to all (1000+)Shaggy Hiker is a name known to all (1000+)Shaggy Hiker is a name known to all (1000+)Shaggy Hiker is a name known to all (1000+)Shaggy Hiker is a name known to all (1000+)Shaggy Hiker is a name known to all (1000+)Shaggy Hiker is a name known to all (1000+)
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
Shaggy Hiker is offline   Reply With Quote
Old Mar 3rd, 2010, 11:40 AM   #5
cicatrix
PowerPoster
 
cicatrix's Avatar
 
Join Date: Dec 09
Location: Moscow, Russia
Posts: 2,237
cicatrix is a glorious beacon of light (400+)cicatrix is a glorious beacon of light (400+)cicatrix is a glorious beacon of light (400+)cicatrix is a glorious beacon of light (400+)cicatrix is a glorious beacon of light (400+)
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.
__________________
#define true ((rand() % 2)? true: false) // Debug THAT!

cicatrix is offline   Reply With Quote
Old Mar 3rd, 2010, 12:12 PM   #7
bergerkiller
Member
 
Join Date: Jul 09
Posts: 51
bergerkiller is an unknown quantity at this point (<10)
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.
bergerkiller is offline   Reply With Quote
Old Mar 3rd, 2010, 12:42 PM   #8
NickThissen
PowerPoster
 
Join Date: Apr 07
Location: The Netherlands
Posts: 4,053
NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)
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.
__________________
Controls:
*NEW* Double TrackBar - Editable ListBox - Outlook Navigation Bar - ColorListBox with images - Advanced ToolStripContainer - RadioButtonGroup Control - Expandable Groupbox - Wizard Template Usercontrol (full Design-time support!) ... Now with Aero Glass support! - 3D Separator - ListView Options Screen - TabControl with tab-specific ContextMenuStrips and Tab-Dragging

Menustrip and Toolstrip Renderers:
Visual Studio 2010 - Customizable Menu/ToolStrip (incl Office 2007 + all Office 2003 styles) - Office 2007 - Visual Studio 2008 - [WIP]Vista Toolstrip

Misc:
*NEW* Snapping windows - *NEW* Advanced Shape Editor like Visual Studio - ByVal vs ByRef and value types vs reference types - Game Of Life - OOP Tic Tac Toe game example - Moving and Resizing a Control or a Borderless Form, using SendMessage (smooth) - Manual 'MDI Window List' menu - Tabbed MDI Text Editor - Very Extensive MDI text editor - Real Synchronized RichTextBox scrolling - Notepad-like New/Open/Save/SaveAs/Exit behaviour
NickThissen is offline   Reply With Quote
Old Mar 3rd, 2010, 12:47 PM   #9
bergerkiller
Member
 
Join Date: Jul 09
Posts: 51
bergerkiller is an unknown quantity at this point (<10)
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.
bergerkiller is offline   Reply With Quote
Old Mar 3rd, 2010, 01:34 PM   #10
NickThissen
PowerPoster
 
Join Date: Apr 07
Location: The Netherlands
Posts: 4,053
NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)NickThissen is a splendid one to behold (700+)
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.
__________________
Controls:
*NEW* Double TrackBar - Editable ListBox - Outlook Navigation Bar - ColorListBox with images - Advanced ToolStripContainer - RadioButtonGroup Control - Expandable Groupbox - Wizard Template Usercontrol (full Design-time support!) ... Now with Aero Glass support! - 3D Separator - ListView Options Screen - TabControl with tab-specific ContextMenuStrips and Tab-Dragging

Menustrip and Toolstrip Renderers:
Visual Studio 2010 - Customizable Menu/ToolStrip (incl Office 2007 + all Office 2003 styles) - Office 2007 - Visual Studio 2008 - [WIP]Vista Toolstrip

Misc:
*NEW* Snapping windows - *NEW* Advanced Shape Editor like Visual Studio - ByVal vs ByRef and value types vs reference types - Game Of Life - OOP Tic Tac Toe game example - Moving and Resizing a Control or a Borderless Form, using SendMessage (smooth) - Manual 'MDI Window List' menu - Tabbed MDI Text Editor - Very Extensive MDI text editor - Real Synchronized RichTextBox scrolling - Notepad-like New/Open/Save/SaveAs/Exit behaviour
NickThissen is offline   Reply With Quote
Reply

Tags
class, dialog, display, library

Go Back   VBForums > Visual Basic > Visual Basic .NET


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 07:57 PM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.