How to associate 2 forms in ONE class?

Hello All,

I have a dilemma, and I'd appreciate any help any one can offer. I'm a little new to Visual BASIC .NET, as I come from mainly a Java background. My problem is this : I have this huge number of classes right, and they mostly all have forms that take input or have buttons for specific choices to be made by a user. Now, for a part of this whole project, I'd like to nest a class which has a simple form associated with that class, which takes in a number from the user, inside another class which I want to call when the user hits a button on the form associated with the "outer" class. So basically, what's gonna happen is I need to have 2 forms associated with one class. Is this possible in VB? I intend to create an instance of the inner class inside the outer class. Does this make sense to anyone? Or does this sound like gibberish? I'm pretty sure this is possible in Java. Thanks.

Sean

[960 byte] By [KissDaFeetOfSean] at [2007-12-16]
# 1
You can only inherit atmost of one class, per class.
In this case, you'd have to keep local variables in the main class representing each form.
Class Form1
'Class Code here
End Class
Class Form2
'Class code here
End Class
Class Main
Public MyForm1 as Form1
Public MyForm2 as Form2
End Class
This way is just publci variables, but you can make them public properties, or functions as well.
Class Main
Private MyForm1 as Form1
Private MyForm2 as Form2

Public Property Form1() As Form1
Get
return MyForm1
End Get
Set(ByVal value As Form1)
MyForm1= value
End Set
End Property
End Class

Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
In VB.NET (and C#, managed C++, J#), there is a one-to-one mapping between Forms and classes. Although what you're talking about is theoretically possible, the visual Forms designer will likely get very upset with you. You'll also confuse future maintainers of the code who are used to thinking of the one-to-one mapping between forms and classes. I would recommend subclassing a base class, which derives from System.Windows.Forms.Form. Keep all common logic in there. You may also want to consider the UI Process Application Block to manage form navigation if you have a lot of forms. MSDN article can be found here:

http://msdn.microsoft.com/msdnmag/issues/05/08/UIPApplicationBlock/default.aspx

JamesKovacs at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
Ok, so I tryed it out to see what would happen. I appears that it works perfectly. All I did was copy and paste. But this still begs the question... since the inner class has a form associated with it, if I switch to design view for the outer class, all I get is the "design view" of the outer class. So i guess all the code and implementation for the inner class form is inside the "Windows Generated Code" section. But then say I would need to be able to modify that form for the inner class later on? How would I ever get at the actual form? This is kinda a tradeoff I suppose between reducing the total number of classes versus maintainability... All the form for the inner class does is just take in a simple number and let the user click 'OK' though... I need this as a class, because I was told that a simple "MessageBox" won't let u take in a number or input from the user. Any thoughts?

Sean

KissDaFeetOfSean at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
The forms designer only understands the one-to-one mapping between forms and classes. It doesn't understand inner classes as forms. If you want to make modifications to your inner form later on using the forms designer, you'll have to copy/paste the inner class into its own separate .vb file, make the modifications, and then copy it back. Your other option would be to not use the forms designer, but do everything manually. All the code read and written by the Forms designer is encapsulated in the outer class's InitializeComponent method.
JamesKovacs at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
Thanks James,
What you said makes perfect sense; your post was extremely helpful, as these forms and their associated classes are a little new to me, as I'm used to using command-line based interfaces to test out code. Thanks again.

Sean

KissDaFeetOfSean at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6
Forgive me if I've misunderstood but I thnk you can extend James example just slightly and be able to design all forms (inner and outer) without the need for any copy/paste of code.

For example:
In a new project, I add 3 new forms called, InnerForm1, InnerForm2 and OuterForm.

In the code for OuterForm you again declare a local field (or as James points out you could use properties):

Public Class OuterForm
Private inner1 as InnerForm1
Private inner2 as InnerForm2
End CLass

Now the code for the inner forms live in their own code files with their own designers and you cann access/modify any of their code from within InnerForm.

Hope that helps.
Cameron McColl
Microsoft

cmccoll_MS at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...