-
Mar 3rd, 2010, 08:22 AM
#1
Thread Starter
Fanatic Member
-
Mar 3rd, 2010, 08:46 AM
#2
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
-
Mar 3rd, 2010, 09:39 AM
#3
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:
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:
Now, use f instead of the name Form1.
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.
-
Mar 3rd, 2010, 10:30 AM
#4
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
-
Mar 3rd, 2010, 12:40 PM
#5
Re: Custom dialog in a class library
Originally Posted by NickThissen
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.
-
Mar 3rd, 2010, 12:48 PM
#6
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.
-
Mar 3rd, 2010, 01:12 PM
#7
Thread Starter
Fanatic Member
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 01:38 PM.
-
Mar 3rd, 2010, 01:42 PM
#8
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.
-
Mar 3rd, 2010, 01:47 PM
#9
Thread Starter
Fanatic Member
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 02:08 PM.
-
Mar 3rd, 2010, 02:34 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|