singelton problem
-
-
Hi, im using a singelton pattern to ensure that only one innstance of a winfrom can be opened at one time. The problem im having is when I close the form and want to open it again I get a "Cannot access a disposed object." How can I solve this? //thx
Hi, if you want to see the same instance of your form class again, you should hide it instead of closing it; so use Form.Hide() or Form.Visible=false :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Hi, if you want to see the same instance of your form class again, you should hide it instead of closing it; so use Form.Hide() or Form.Visible=false :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Thx, but I don't want to show the same instance of the form. I wan't to be able to create a new instance but only have one open at the same time.
I think you need to show some code, with form creation, Dispose, your current' singleton stuff, etc. And what should (not) cause the creation of a new form ? :)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Thx, but I don't want to show the same instance of the form. I wan't to be able to create a new instance but only have one open at the same time.
-
I think you need to show some code, with form creation, Dispose, your current' singleton stuff, etc. And what should (not) cause the creation of a new form ? :)
Luc Pattyn [My Articles] [Forum Guidelines]
So some samplecode then. when the user clicks the button the new form opens. He should not be able to open any more forms of this type unless he closes the form. inside main form: private void button1_Click(object sender, EventArgs e) { Form2 form = Form2.Instance(); form.Show(); } inside form2: private static Form2 instance; public static Form2 Instance() { if (instance == null) { instance = new Form2(); } return instance; } protected Form2() { }
-
So some samplecode then. when the user clicks the button the new form opens. He should not be able to open any more forms of this type unless he closes the form. inside main form: private void button1_Click(object sender, EventArgs e) { Form2 form = Form2.Instance(); form.Show(); } inside form2: private static Form2 instance; public static Form2 Instance() { if (instance == null) { instance = new Form2(); } return instance; } protected Form2() { }
Hi, from your code it is apparant you implemented a singleton: initially there is no instance of Form2; once you call Form2.Instance, there is exactly one. If you call Form2.Instance again, you get the same instance. This is not what you want for two reasons: - if you close the "first" Form2, it renders all future Form2 instances useless since you can close a Form only once - it does not prevent you from calling Form.Show() twice. What you need instead is no singleton logic; just create a Form2 when you need one; use it; get rid of it with Close(). And add some logic to prevent two Form2 instances to coexist; possible solutions are: - disable the button while a Form2 is visible (best, user can see it!) - or include a test in buttonClick, something like:
private Form2 lastForm2; // class member if (lastForm2==null || !lastForm2.Visible) { lastForm2=new Form2(); lastForm2.Show(); }
:)
Luc Pattyn [My Articles] [Forum Guidelines]
-
Hi, im using a singelton pattern to ensure that only one innstance of a winfrom can be opened at one time. The problem im having is when I close the form and want to open it again I get a "Cannot access a disposed object." How can I solve this? //thx
-
Form1 form1;
...
...
...if(form1==null)
Form1 form1 = new Form1();
form1.Show();And this's all it takes!
Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)
Hi Muammar, almost; you should drop one "Form1" !
Luc Pattyn [My Articles] [Forum Guidelines]
-
Form1 form1;
...
...
...if(form1==null)
Form1 form1 = new Form1();
form1.Show();And this's all it takes!
Smile: A curve that can set a lot of things straight! (\ /) (O.o) (><)
thx, searched for a while and finally found why I didn't get it to work. Apparently I also need to check if the form has been disposed of. private Form2 form2; .. .. private void button1_Click(object sender, EventArgs e) { if (form2 == null || form2.IsDisposed) { form2 = new Form2(); form2.Show(); } }
-
thx, searched for a while and finally found why I didn't get it to work. Apparently I also need to check if the form has been disposed of. private Form2 form2; .. .. private void button1_Click(object sender, EventArgs e) { if (form2 == null || form2.IsDisposed) { form2 = new Form2(); form2.Show(); } }
I believe checking form2.IsDisposed will create the functionality that you are looking for, but from reading this thread, it seems like you are inappropriately applying the Singleton pattern if you are needing to perform this check. You might want to read up on your Singleton & Other GoF patterns[^] The Singleton is not really intended for flow control between forms unless it is necessary that (for some reason) you require the EXACT SAME instance of the form throughout the entire application process.(Singletons are more appropriately applied(for example) to concurrent invocation of a state machine on a remote server....or for the Load Balancing example given at the link I included above) Unless you have a very good reason for doing this, then using a Singleton convolutes your logic, and should be avoided. If you are simply wanting to restrict your user to only maintaining one instance of the form at any one time, then you could simply disable the ability to create a separate instance as was demonstrated in previous replies
Allow user to instantiate a new form(You're probably wanting it to be Modal)
Restrict user from instantiating a second instance of the form
User invokes some business logic on the form
User closes form when finishedIf the user wants to replicate the process(let's say...the form allows the user to perform some calculation based upon values selected in an UltraWebGrid), the process would be repeated.
Welcome my son...Welcome..to the Machine