Open Form question

silverstormboy

Newcomer
Joined
Nov 22, 2002
Messages
18
Location
Singapore
Hi, I have a simple question.

I have two forms : form 1 and form 2.

Form 1 has one button(button1).
If i click button1, i want form 2 to be opened.

I know that we should declare variable like this:

Dim M as new form2
M.show()

it will create an object form2.

but if i click over and over again, it will create a new form2 again and again.

My question is: how to prevent the button(button1) from creating a New form2?

in another words, if Form2 is still active, clicking on button1 will redirect to the current Form2.

I hope you will understand my question.

thank you for your reply :)
 
You'd need to keep a variable stored in Form1, of type Form2. Declare it at form-level scope:

Visual Basic:
Private objForm As Form2

Then when the time comes to open/show it:

Visual Basic:
If objForm Is Nothing Then objForm = New Form2()
objForm.Show()
objForm.BringToFront()

Something like that ought to do the trick.
 
Hi Divil, i have another question to ask.

there is a close button on Form2(objForm).


when i click the close button, the form2(objform) is closed successfully. (the code inside the close button is : Me.close() )

but when i try to reopen Fom2 for the second time, it didnt work.

it said:
(An unhandled exception of type 'System.ObjectDisposedException' occurred in system.windows.forms.dll
Additional information: Cannot access a disposed object named "Form2").

is there something wrong with the code?

Thanks for your help :)
 
Ah, I see the problem. You have one of two options, really.

If you really want the same instance of the form to be kept around, and made either visible or hidden (this is what I assumed you meant in your first question) you will have to avoid the Close method, and use Hide instead.

If you want there to be a new instance created when the user opens it again after properly closing it (with the Close method) you will have to modify the code I gave you originally to detect if it has been disposed of, and if so, create a new instance:

Visual Basic:
If objForm Is Nothing Then
  objForm = New Form2()
ElseIf objForm.IsDisposed Then
  objForm = New Form2()
End If
objForm.Show()
objForm.BringToFront()
 
bravo...
It works again :)

just wondering, how do you find the "IsDisposed" properties?,because i cant see it in the list when you type "." after objForm.

Do you know that properties by experience?

any way, thank you very much for your answer. have a nice day :)
 
Dude, sweet!!

Thanx Divil, this accelerates the development of my my projects considerably...

Not to mention, givs me some nifty ideas...
 
Tools > Options > Text Editor > All Languages

Uncheck "Hide advanced members" and all members will be displayed in the IntelliSense.
 
Just a Quick Note...

I found today that when you type in the reference to another form or other object in code, the code viewer ( at least in VB.NET ) will often add a set of parenthesis to the end of the object name...

It's no big deal as long as you go back and delete them...

You type:
Visual Basic:
Private myObj as Form1
and you get:
Visual Basic:
Private myObj as Form1()

Just go back and take off the parenthesis to get:
Visual Basic:
Private myObj as Form1
and everything works just fine...
 
Back
Top