VB multiple forms

person31

Estimable
Jun 13, 2015
5
0
4,510
Hi, I have created a VB rpoject with 3 forms. 1 needs to be invisible and the other 2 need to be visible. When I run the project, none are visible, even if I put me.visible = True on the two visible forms. Can somebody help me? Thanks
 
Solution
A VB form is essentially just a class. A class is the design of an object, not the object itself. An object must be 'instantiated" using the class to have an object that can be manipulated, e.g. shown on the screen.

I suspect you have your VB project set up to use the one form that is visible as the "Startup Object". That causes VB to generate the code to instantiate an object using that form's design. So that is the one that is displayed because its "Visible" property is set to True.

Most properties of a class can be changed at run time. When you set the property in the form design you are just setting the default property. So any time an instance of the class is created it will already have its Visible property set and it will be...

thx1138v2

Distinguished
Jun 18, 2011
74
1
18,610
A VB form is essentially just a class. A class is the design of an object, not the object itself. An object must be 'instantiated" using the class to have an object that can be manipulated, e.g. shown on the screen.

I suspect you have your VB project set up to use the one form that is visible as the "Startup Object". That causes VB to generate the code to instantiate an object using that form's design. So that is the one that is displayed because its "Visible" property is set to True.

Most properties of a class can be changed at run time. When you set the property in the form design you are just setting the default property. So any time an instance of the class is created it will already have its Visible property set and it will be displayed.

Your other two forms _default_ Visible properties are set to True but an instance of these forms has not been created because VB has only instantiated the one designated as the "Startup Object". You must write the code to instantiate the other forms. Assuming the names of your forms are "Form2" and "Form3" Something like this:

Dim objForm2 as Form2
Dim objForm3 as Form3

Set objForm2 = New Form2
Set objForm3 = New Form3

The first two line declare variables that will hold a reference to the form object. The last two lines tell VB to create instances of those forms and populate those variables with the references that VB creates.

You still won't have them displayed though. The instances exist but they must be told to display themselves with:
objForm2.Show
objForm3.Show

You can do this in the Form_Load() event of Form1 or in response to some button click on Form1 or some other action.

When you want to close the forms you can code the following to destroy the instances of them that you created:
Set objForm2 = Nothing
Set objForm3 = Nothing

and they will disappear because they have been destroyed or you can code
objForm2.Visible = False
objForm3.Visible = False

and they will disappear but they will still exist. So you can then code:
objForm2.Visible = True
objForm3.Visible = True

and they will be displayed again.

You need to find a good tutorial on whichever version of VB you are using. There is far too much to discuss in a forum. So I won't be answering any more questions you may have until you get further along. I just wanted to get you started.
 
Solution