Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 :)

  • *Gurus*
Posted

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

 

Private objForm As Form2

 

Then when the time comes to open/show it:

 

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

 

Something like that ought to do the trick.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

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 :)

  • *Gurus*
Posted

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:

 

If objForm Is Nothing Then
 objForm = New Form2()
ElseIf objForm.IsDisposed Then
 objForm = New Form2()
End If
objForm.Show()
objForm.BringToFront()

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

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 :)

Posted

Dude, sweet!!

 

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

 

Not to mention, givs me some nifty ideas...

UCM

>-)

Posted

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:

Private myObj as Form1

and you get:

Private myObj as Form1()

 

Just go back and take off the parenthesis to get:

Private myObj as Form1

and everything works just fine...

UCM

>-)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...