silverstormboy Posted January 18, 2003 Posted January 18, 2003 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 :) Quote
*Gurus* divil Posted January 18, 2003 *Gurus* Posted January 18, 2003 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. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
silverstormboy Posted January 18, 2003 Author Posted January 18, 2003 Thanks a lot Divil....it works :) Quote
silverstormboy Posted January 19, 2003 Author Posted January 19, 2003 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 :) Quote
silverstormboy Posted January 19, 2003 Author Posted January 19, 2003 oh , one more thing. when the error occured, it highlight the: objForm.show() Hope this wil help you find my mistake....Thanks :) Quote
*Gurus* divil Posted January 19, 2003 *Gurus* Posted January 19, 2003 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() Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
silverstormboy Posted January 19, 2003 Author Posted January 19, 2003 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 :) Quote
UCM Posted January 20, 2003 Posted January 20, 2003 Dude, sweet!! Thanx Divil, this accelerates the development of my my projects considerably... Not to mention, givs me some nifty ideas... Quote UCM >-)
*Gurus* divil Posted January 20, 2003 *Gurus* Posted January 20, 2003 Tools > Options > Text Editor > All Languages Uncheck "Hide advanced members" and all members will be displayed in the IntelliSense. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
UCM Posted January 27, 2003 Posted January 27, 2003 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... Quote UCM >-)
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.