Disabling multiple instances of a MDIChild form.

V

VM

Hi,

Is it possible to open always only one instance of a MDIChild form?
Say, if form A is already opened, just give it the focus.
I tried declaring theMDIChild forms as private members of the MDI form
(using the MDI menu's script), but when the MDIChild form is closed (x
button pushed), I cannot open again the MDIChild form.

Example:

// aForm is a private member of the MDI form.

if (aForm.Visible)
aForm.Activate();
else
aForm.Show();

// This works nice once, and the form gets the focus once it's already
opened, but if it's closed *once* it doesn't open again.
 
N

Nicholas Paldino [.NET/C# MVP]

VM,

I would have a factory method/property that would handle this for you,
something like this:

// Stores the instance of the current form.
private static Form form;

public static Form GetForm()
{
// If the form is null, create it here.
if (form == null)
{
// Create it here.
form = new Form();

// Handle the closed event, when closed, set the form to null.
// This syntax is for .NET 2.0. You can just create a delegate and
add that
// to the form for previous versions.
form.Closed += delegate
{
// Set form to null.
form = null;
};
}

// Return the form.
return form;
}

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

// Declare constructor private. This probably will give you designer
problems though, so
// you don't have to do this.
 
M

MDeiters

Make the Form constructor private. Then create a static (shared) method
on the form called GetInstance() which returns an internal static
(shared) variable holding an instance of the form
 
N

Nicholas Paldino [.NET/C# MVP]

I believe that setting the form constructor to prviate will mess up the
designer support, so that's probably not a good idea.
 
N

Nicholas Paldino [.NET/C# MVP]

Apparently, I was mistaken. It works just fine.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nicholas Paldino said:
I believe that setting the form constructor to prviate will mess up the
designer support, so that's probably not a good idea.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Make the Form constructor private. Then create a static (shared) method
on the form called GetInstance() which returns an internal static
(shared) variable holding an instance of the form
 
M

Matt Deiters (www.theAgileDeveloper.com)

did you try it? Probably not since it does work. About the factory
method, that works but that does ensure that "always only one instance"
of the form exists. Some developer could join the team and start
instaintiating the form all over the place and never use your factory.
I am not a fan of singletons but if you need to make sure that no other
instances of the form exists then you need to change the constructor's
access modifier.
 
V

VM

Thx Nicholas, I like Matt's approach. It works.

Nicholas Paldino said:
I believe that setting the form constructor to prviate will mess up the
designer support, so that's probably not a good idea.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Make the Form constructor private. Then create a static (shared) method
on the form called GetInstance() which returns an internal static
(shared) variable holding an instance of the form
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top