Calling a form from code

Rajya

Member
Joined
Apr 15, 2005
Messages
22
Location
Calcutta, India
Programming Experience
1-3
Hi

In VB 6.0, if I needed to call a Form2 from a Form1, I just put in the following command in, say Form_Load of Form1 and it would work -

Private Sub Form_Load()
Form2.Show
End Sub

When I upgrade this project to VB.Net 2003, the same is upgraded without any issues as follows -

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Form2.DefInstance.Show()
End Sub

But when I am developing a native project/solution in VB.Net, I am not able to call the Form2 as I did it in VB 6.0; I know it is done differently here, but I am unable to figure out how.

Any help would be hugely appreciated :)
 
Last edited:

smiling4ever

New member
Joined
Apr 15, 2005
Messages
2
Programming Experience
5-10
Easy, dont worry

Hello,

You have to know that VB.NET is full Object Oreinted. So you nead to create a new instance of the form2,

let assume the form2 name is form2
then from the form1 , you can call it as the following

Dim newform2 as new form2
newform2.show



Hope that help
bye
 

Rajya

Member
Joined
Apr 15, 2005
Messages
22
Location
Calcutta, India
Programming Experience
1-3
Calling a Form from code

Yes, it did help!
Thanks a ton - I have been able to get it working now :)

But since I am new to .NET (even though have worked with Java OOP), I am not able to get to terms with the idea that I am unable to display the form I have actually designed, instead I have to use a copy.

Further, please tell me - would all the properties, including the Name would be the same as the original object or do I have to adapt them further for actual use?

Thanks in advance
 

Rajya

Member
Joined
Apr 15, 2005
Messages
22
Location
Calcutta, India
Programming Experience
1-3
Calling a Form from code

Thanks everybody, for your help.
I am now trying my hand at all these issues.
Hope to get your attention in the future too :)
 

Rajya

Member
Joined
Apr 15, 2005
Messages
22
Location
Calcutta, India
Programming Experience
1-3
Calling a Form from code

Hi

I have been able to instantiate my Form & am able to Show it now.

But this Form (personal information entry) and there would be others like it, would be used quite often in my application - each time it would be closed and then the user would want to call it again for another phase of data entry.

I have been using .Close and .Dispose to close the Form but when I try .Showing it again, it gives an error about the instance not being there (obviously, because it was Closed).

So how do should I go about handling this situation? I have put in the following line in a Module, and .Show the "frmPersonalData" instance from a MDI Parent Form -

Public frmPersonalData as New PersonalData

I even put the line in a new Class declaration in the same Module but then the "frmPersonalData" instance was not found.

What should I do?

TIA
 

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,524
Location
Lansing, MI; USA
Programming Experience
10+
if you declare the form variable at the top (module level) IE:
VB.NET:
" Windows Generated Code "

Private frm2 as New form2

Private Sub frmMain_Load(...) Handles MyBase.Load

End Sub

you can use frm2.ShowDialog and it'll always work (even after you've closed it elsewhere)
 

Rajya

Member
Joined
Apr 15, 2005
Messages
22
Location
Calcutta, India
Programming Experience
1-3
Hi

Sorry but I did not quite get you.
I have already declared the following line at the top of a Module.

Public frmPersonalData as New PersonalData

The scenario is like this - I have a MDI Parent Form1 with a Child Form2 (frmPersonalData) attached. When I click a particular menu item on Form1, the Form2 is .Shown.

Subsequently, I would like to shut down Form2 (.Close / .Dispose) and open another MDI child Form3. So far so good, but when I try reopening Form2, an error comes up (since Form2 is already disposed).

How do I tackle this situation so that I am able to re-open Form2. Hiding / Showing again is not desirable.

Any ideas?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,328
Location
Sydney, Australia
Programming Experience
10+
If you Dispose an object it no longer exists. That means it is not possible to display it again without creating another instance of the same class. If you are having trouble with the OO concepts involved, assume your forms are biscuits. You know what a biscuit is, but you can't show someone one unless you have an instance of a biscuit in your hand. If you dispose of that biscuit, you can't then show it to someone (OOP doesn't let you get it back out of the rubbish). You have to get a new instance of a biscuit to show them. If you want to show them the original biscuit, you must not dispose of it, but rather hide it.

In short, if you want to repeatedly show the same instance of a Form, you must Hide it rather than Close it. If it does not have to be the same instance, i.e. you do not need to keep the data from one showing to another, you can Close the form and then create a New instance each time you want to Show it. If you choose to Hide but don't want to keep the previous data, put some code in the Activated event handler to clear the forms data fields.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,328
Location
Sydney, Australia
Programming Experience
10+
Rajya said:
Hi

Sorry but I did not quite get you.
I have already declared the following line at the top of a Module.

Public frmPersonalData as New PersonalData

The scenario is like this - I have a MDI Parent Form1 with a Child Form2 (frmPersonalData) attached. When I click a particular menu item on Form1, the Form2 is .Shown.

Subsequently, I would like to shut down Form2 (.Close / .Dispose) and open another MDI child Form3. So far so good, but when I try reopening Form2, an error comes up (since Form2 is already disposed).

How do I tackle this situation so that I am able to re-open Form2. Hiding / Showing again is not desirable.

Any ideas?

I've looked at this again and will refer to it specifically. The line

Public frmPersonalData As New PersonalData

declares the variable and assigns a new instance to it. When you click the menu option, that instance is displayed. When the form is closed that instance is disposed. When you click the menu option again you attempt to show a disposed object. Use this code instead:
VB.NET:
'Declare the form variable but do not instantiate it.
'This means that we know the variable will refer to a PersonalData object but it doesn't yet.
Dim frmPersonalData As PersonalData

...

Private Sub menuItem_Click(...) Handles menuItem.Click
	'Instantiate the form each time you want to show it.
	'This means that the variable now refers to an instance of the PersonalData class.
	frmPersonalData = New PersonalData

	'Show the form.
	frmPersonalData.Show() 'or .ShowDialog() for modal dialog.
End Sub
 
Top Bottom