How to only open one instance of a window form from parent form
-
I have tried using the mdi method, but found out that it wasnt what I wanted. It opens a form within the parent form, which isnt the way I want What I want to do is to open a form from a parent form. If I use form.Show(); I can go back to the parent form and open the same form again. That I want not possible. If I use form.ShowDialog(); I cant go back to the parent form, which is not what I want either. What I want to do is to be able to go back to the parent form and open other forms, but not open the same form twice. How do I do that?
-
I have tried using the mdi method, but found out that it wasnt what I wanted. It opens a form within the parent form, which isnt the way I want What I want to do is to open a form from a parent form. If I use form.Show(); I can go back to the parent form and open the same form again. That I want not possible. If I use form.ShowDialog(); I cant go back to the parent form, which is not what I want either. What I want to do is to be able to go back to the parent form and open other forms, but not open the same form twice. How do I do that?
For that you have to create a globally accessible class. In that just put bool variable for each form like : IsForm1Open=false; IsForm2Open=false; IsForm3Open=false; Before opening any Form from the MDI first check this variable. And on the each form's load event make that variable true. and in the dispose method make this false. ;)
Regards Pankaj Joshi
-
For that you have to create a globally accessible class. In that just put bool variable for each form like : IsForm1Open=false; IsForm2Open=false; IsForm3Open=false; Before opening any Form from the MDI first check this variable. And on the each form's load event make that variable true. and in the dispose method make this false. ;)
Regards Pankaj Joshi
Why are you doing this? An MDI form contains a list of child forms. Use that to evaluate whether or not the form is present. A simplistic example would be:
bool found = false; foreach (Form f in this.MdiChildren) { if (f.Text == "RibbonForm1") { found = true; f.TopMost = true; } } if (!found) { RibbonForm1 frm = new RibbonForm1(); frm.MdiParent = this; frm.Show(); }
Deja View - the feeling that you've seen this post before.
-
Why are you doing this? An MDI form contains a list of child forms. Use that to evaluate whether or not the form is present. A simplistic example would be:
bool found = false; foreach (Form f in this.MdiChildren) { if (f.Text == "RibbonForm1") { found = true; f.TopMost = true; } } if (!found) { RibbonForm1 frm = new RibbonForm1(); frm.MdiParent = this; frm.Show(); }
Deja View - the feeling that you've seen this post before.
Good solution ;) But this is OK when I am having more than 100 forms in my application. this will loop for each time 100 times..? Performance issue...:rolleyes:
Regards Pankaj Joshi
-
Why are you doing this? An MDI form contains a list of child forms. Use that to evaluate whether or not the form is present. A simplistic example would be:
bool found = false; foreach (Form f in this.MdiChildren) { if (f.Text == "RibbonForm1") { found = true; f.TopMost = true; } } if (!found) { RibbonForm1 frm = new RibbonForm1(); frm.MdiParent = this; frm.Show(); }
Deja View - the feeling that you've seen this post before.
-
Good solution ;) But this is OK when I am having more than 100 forms in my application. this will loop for each time 100 times..? Performance issue...:rolleyes:
Regards Pankaj Joshi
I'd just create a central form pool somewhere and use id's to fetch the form I want. Example:
public class FormpoolClass
{
private Dictionary< string, Form > formPool = new Dictionary< string, form=>();public class CustomForm : Form { } public formType GetForm<formType>(string form\_id) where formType : Form { if (this.formPool.ContainsKey(form\_id)) // check out if we have a reference to the form { if (formPool\[form\_id\] != null && !formPool\[form\_id\].IsDisposed) return formPool\[form\_id\] as formType; // if so, return it else formPool.Remove(form\_id); } formType ret = null; try { ret = Activator.CreateInstance<formType>(); // not there, so create it } catch (Exception e) { Console.WriteLine(e.ToString()); } Debug.Assert(ret != null); return ret; } } public class Test { public void dotest() { FormpoolClass fpc = new FormpoolClass(); CustomForm f = fpc.GetForm<customform&rt;("myform\_id"); } }
Standards are great! Everybody should have one!
-
(f.text == "RibbonForm1") what is it? could tell me that? and do i have to set the parent to mdicontainer? if i do that the background colors turn to gray. any way to change to the default color?
This is just a simple way of telling who the child form is. I wouldn't rely on this, as it was only a quick sample, and the text is the thing that appears in the titlebar. You would create this form from the parent using.
MyForm form = new MyForm(); form.Parent = this; form.Show();
Deja View - the feeling that you've seen this post before.
-
I'd just create a central form pool somewhere and use id's to fetch the form I want. Example:
public class FormpoolClass
{
private Dictionary< string, Form > formPool = new Dictionary< string, form=>();public class CustomForm : Form { } public formType GetForm<formType>(string form\_id) where formType : Form { if (this.formPool.ContainsKey(form\_id)) // check out if we have a reference to the form { if (formPool\[form\_id\] != null && !formPool\[form\_id\].IsDisposed) return formPool\[form\_id\] as formType; // if so, return it else formPool.Remove(form\_id); } formType ret = null; try { ret = Activator.CreateInstance<formType>(); // not there, so create it } catch (Exception e) { Console.WriteLine(e.ToString()); } Debug.Assert(ret != null); return ret; } } public class Test { public void dotest() { FormpoolClass fpc = new FormpoolClass(); CustomForm f = fpc.GetForm<customform&rt;("myform\_id"); } }
Standards are great! Everybody should have one!
That's exactly how I'd do it as well. Have yourself a 5 sir.
Deja View - the feeling that you've seen this post before.
-
Good solution ;) But this is OK when I am having more than 100 forms in my application. this will loop for each time 100 times..? Performance issue...:rolleyes:
Regards Pankaj Joshi
And you are relying on adding a new condition and test everytime you add a new form. Hmm. Plus, how many times do you have 100 forms in an app? More importantly, as Bekjong states, I wouldn't rely on a loop test in my code. Instead, I'd use a generic dictionary to manage this. Then your code becomes a lot simpler and a lot faster.
Deja View - the feeling that you've seen this post before.
-
I have tried using the mdi method, but found out that it wasnt what I wanted. It opens a form within the parent form, which isnt the way I want What I want to do is to open a form from a parent form. If I use form.Show(); I can go back to the parent form and open the same form again. That I want not possible. If I use form.ShowDialog(); I cant go back to the parent form, which is not what I want either. What I want to do is to be able to go back to the parent form and open other forms, but not open the same form twice. How do I do that?
-
Why are you doing this? An MDI form contains a list of child forms. Use that to evaluate whether or not the form is present. A simplistic example would be:
bool found = false; foreach (Form f in this.MdiChildren) { if (f.Text == "RibbonForm1") { found = true; f.TopMost = true; } } if (!found) { RibbonForm1 frm = new RibbonForm1(); frm.MdiParent = this; frm.Show(); }
Deja View - the feeling that you've seen this post before.