I'm sure one or two of you have come accros this and the answer is probably staring me right in the face, but how do I get the child forms to be shown once, for example....
.... I have my MDI Form, a menu etc under one of my menu items "&Help" I have, "&About", "&Support" and "&Licence" now I only what each of these forms to be shown once.
I have tried: about f = new about();
f.MdiParent = this;
f.ShowDialog();
but this just throughts an exception (cant remember of the top of my head) some thing like this form can not be displayed in this manor.
When I get home I'll make a note of the error and post it here (if I remember).
When you want to show the forms modally by using ShowDialog() method, you don't need to assign MDI Parent to them. Simply comment the line "f.MdiParent = this;" from your code, and it should work just fine.
Scott McKeown
Posted: Visual C# Express Edition, MDI Child Once Only please
Thanks for the reply, but that does not solve the problem.
This would create the child from outside of the MDI Pain, which I don't want, I still need the child form to be contained within its parent, have the normal min,max close and to show in the windows menu list. What I dont want is two copies of the same child form to be open at the same time.
Scott
Blair Allen Stark
Posted: Visual C# Express Edition, MDI Child Once Only please
try this pattern . . . Use "Visual Inheritance." a baseform class has a static dictionary that keeps track of singleton instances of inherited forms and their WindowState.
Create a new Windows Forms application project - SingletonForms
Add a new Windows form - BaseForm.cs
Build the project
add a new Windows form- Server1.cs, but select "Inherited form" from the templates and select BaseForm.
repeat step 4, calling the file Server2.cs
Change the original form1 to an MdiContainer.
Place a menu strip on the form add a top level menu entry "Servers" with child menu items - Server1 and Server2
Put this code in form1.cs:
Ancalagon
Posted: Visual C# Express Edition, MDI Child Once Only please
Hi, I think I was having the same problem as you only that I didn’t have a MDI form What I wanted: to click some button (About for example) everytime I wanted but the result is one and only one instance of the Form that button calls, Exactly the same way the "Insert->Symbol" works in MSWord look for my thread and the solution a guy gave to me. I user the second solution from the two he gave. I hope that works:
Gotcha, what you need to do is before you show your child form, you need to make sure it is not already shown. You can check this by looking into MdiChildren property of the MDI form. If form is available in that collection you can simply activate it, otherwise you can create a new instance of the form and update its MdiParent property. Add following function to your MDI form:
intenal void ShowForm<T>() where T : Form, new()
{
foreach (Form form in MdiChildren)
{
if (form.GetType() == typeof(T))
{
form.Activate();
form.WindowState = FormWindowState.Normal;
}
}
T formToShow = new T();
formToShow.MdiParent = this;
formToShow.Show();
}
and in your child form menu click handlers you can make calls to this functions, e.g
ShowForm<Form1>();
ShowForm<AboutForm>();
Hope this helps, let me know if otherwise.
nobugz
Posted: Visual C# Express Edition, MDI Child Once Only please
This code does not seem to work for me, can you just confirm that in the click even to my menu items, for example the Help option i would put under the click event handler
ShowForm<MDI>(); // this is the name of my mdi parent
ShowForm<help>(); // this is the name of my help form
this is what I'm having problems with.
Any thoughts, thanks again
Scott
Scott McKeown
Posted: Visual C# Express Edition, MDI Child Once Only please
Yes,
this is only applicable to forms shown as a dialog. If you want
MDI child forms to be shown non-modally only once, you'll have to do a
bit more work: