Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open & Close forms in VB.NET

Status
Not open for further replies.

gordonwilson

Technical User
Oct 25, 2003
1
GB
Hi

I am new to VB.NET and basically am having problems opening and closing forms.

To open a form this is what I do:

Dim AddEntry as New Form2
AddEntry.Show

To close a form (say after a save operation):

Me.Hide
or
AddEntry.Hide

Now. Here is the tricky part. If I use the X button to close the window the next time the program tries to open it I get the following error "Cannot access a disposed object named "Form2"

Has anyone seen this before?

What am I doing wrong?
 
In most cases you should not hide your Form. You should dispose it and recreate the object each time.

Dim myForm as Form1

myForm = New Form1
myForm.Show



myForm.Dispose


Then just recreate it when needed again

myForm = New Form1
myForm.Show

If you need to keep your form in memory for some reason you could catch the onClosing Event and cancel the event.


DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb
-----------------------------------
If you can't explain it simply, you don't understand it well enough.
- A. Einstein
 
This is also a problem w/ me.

I like to have only one instance of the form. Using

Dim myForm as Form1

myForm = New Form1
myForm.Show


will create many instance of the form. How can I just do that?

AUXilliary COMmunication 1
 
Personally I set a variable like myFormOpen to true when i show the form, and to false when I close it.

Then I add an if block to the show line ....

If myFormOpen = False Then
myFormOpen = True
Dim myForm as Form1
myForm = New Form1
myForm.Show
Else
MsgBox("Can only open one myForm at a time")
End IF

then in the closing event of myForm

myFormOpen = False

Hope that helps.


Becca

Somtimes, the easy answer is the hardest to find. :)

Still under construction ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top