|
|
#1 |
|
Member
Join Date: Jul 09
Posts: 51
![]() |
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 |
|
PowerPoster
Join Date: Dec 09
Location: Moscow, Russia
Posts: 2,237
![]() ![]() ![]() ![]() ![]() |
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!
|
|
|
|
|
|
#3 |
|
PowerPoster
Join Date: Apr 07
Location: The Netherlands
Posts: 4,053
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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() 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 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 |
|
|
|
|
|
#4 |
|
Loquacious User
Join Date: Aug 02
Location: Idaho
Posts: 12,058
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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 | |
|
PowerPoster
Join Date: Dec 09
Location: Moscow, Russia
Posts: 2,237
![]() ![]() ![]() ![]() ![]() |
Re: Custom dialog in a class library
Quote:
OpenFile and SaveFile dialogs are perfect examples to my words.
__________________
#define true ((rand() % 2)? true: false) // Debug THAT!
|
|
|
|
|
|
|
#6 |
|
PowerPoster
Join Date: Apr 07
Location: The Netherlands
Posts: 4,053
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
Re: Custom dialog in a class library
True, for the ShowDialog method it doesn't matter. But for a regular Show it does, so I decided to mention it anyway.
__________________
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 |
|
|
|
|
|
#7 |
|
Member
Join Date: Jul 09
Posts: 51
![]() |
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
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
(it did with a previous library...) Last edited by bergerkiller; Mar 3rd, 2010 at 12:38 PM. |
|
|
|
|
|
#8 |
|
PowerPoster
Join Date: Apr 07
Location: The Netherlands
Posts: 4,053
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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() Code:
Dim f As New TextureBrowseDialog f.Show()
__________________
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 |
|
|
|
|
|
#9 |
|
Member
Join Date: Jul 09
Posts: 51
![]() |
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
Thanks everyone for the great help
Last edited by bergerkiller; Mar 3rd, 2010 at 01:08 PM. |
|
|
|
|
|
#10 |
|
PowerPoster
Join Date: Apr 07
Location: The Netherlands
Posts: 4,053
![]() ![]() ![]() ![]() ![]() ![]() ![]() |
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 |
|
|
|
![]() |
| Tags |
| class, dialog, display, library |
|
||||||
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|