Problem & Solution: Allow only one instance of any MDI child form in your MDI application

My blog has moved.
You can view this post at the following address:
http://www.osherove.com/blog/2004/6/16/problem-amp-solution-allow-only-one-instance-of-any-mdi-chil.html
Published Wednesday, June 16, 2004 9:56 AM by RoyOsherove
Filed under:

Comments

Wednesday, June 16, 2004 8:54 AM by M. Eaton

# re: Problem & Solution: Allow only one instance of any MDI child form in your MDI application

I did it by making my form a singleton. Quite a bit less code and I'm pretty sure we get the same results.
Wednesday, June 16, 2004 9:47 AM by James Roe-Smith

# re: Problem & Solution: Allow only one instance of any MDI child form in your MDI application

The singleton wouldn't work here because I would still expect an application to potentially have two or more instances of the MDI (while still only having a single instance of form X inside each MDI.

Roy's approach seems to work quite OK.

I'd be inclined to create a "Must Inherit" (sorry, I'm a VB programmer) form that implements the single form within an MDI functionality and then inherit all single-only forms from this one.

Use the collection to keep track of instances within the base form as a "shared" variable and used a shared function to return an instance of a form if it can for the MDI for a given type. Then the MDI code is greatly simplified.

Public Shared Function CreateMdiChildInstance(childType as Type, mdi as Form) as Form

I hope that makes sense. Enjoy!
Wednesday, June 16, 2004 1:00 PM by Roy Osherove

# re: Problem & Solution: Allow only one instance of any MDI child form in your MDI application

M.Eaton: A ginleton means it never gets destructed. I want my form do get destructed and not stay in memory throughout the whole application.
Making it a singleton would make me do changes in the form code so that it does not get closed, am I wrong?
Wednesday, June 16, 2004 2:01 PM by Luciano Evaristo Guerche

# re: Problem & Solution: Allow only one instance of any MDI child form in your MDI application

Roy,

Since a Windows Form is an object I guess the Singleton pattern could be applied. In the Microsoft Patterns and Practices it can be found under Application Implementation patterns or directly at:

Implementing Singleton in C#
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpatterns/html/ImpSingletonInCsharp.asp

What do you think about it?
Thursday, June 17, 2004 7:16 AM by Roy Osherove

# re: Problem & Solution: Allow only one instance of any MDI child form in your MDI application

Luciano: I'm aware of the Singleton parttern - however I don't like implemnting it here for a few reasons:
1. I will need to make code changes in all my forms *and* in the calling code. In my way the form has nothing to do with the loading implementation. Less code to debug and maintain.
2. A singleton implies an object that is never Destroyed. Forms are "closed" = destroyed after a while. If I want my form to be a Singleton I would have to write code in my form that implements a "Hide on Close" logic because it will need to stay loaded all the time even if someone closed it. Otherwise the next time you call Show on the form after it has been closed you'll get a nasty "Cannot access a disposed object " error.

I think my way is a little less messy than that plus it keeps all the logic in a separate place.
Thursday, June 17, 2004 7:10 PM by M. Eaton

# re: Problem & Solution: Allow only one instance of any MDI child form in your MDI application

Roy, Ok...in the application I'm working on, I have a search screen. I want one, and only one, instance of this form. If it's minimized, I simply want it to popup the current instance. I can close it, but any way it goes, I will only ever be able to open one instance of it.

I did this by creating a static GetInstance method. If there isn't an active instance, it will create one.

Does that make sense?

mike
Thursday, June 17, 2004 7:15 PM by Roy Osherove

# re: Problem & Solution: Allow only one instance of any MDI child form in your MDI application

M.Eaton: sure. Sounds like what I just implemented. But that's not a Singleton. It's a static factory. Where is the method sitting? inside the form? How is it keeping track of whether an instance is alive or not?
Thursday, June 17, 2004 7:26 PM by DarthPedro

# re: Problem & Solution: Allow only one instance of any MDI child form in your MDI application

This was an interesting way to solve the problem. Thanks.
Thursday, June 17, 2004 7:47 PM by M. Eaton

# re: Problem & Solution: Allow only one instance of any MDI child form in your MDI application

Hehe...yea, as I was typing the last post, I asked myself "am I using the correct terminology?"...live and learn. :-)
Thursday, June 17, 2004 7:50 PM by M. Eaton

# re: Problem & Solution: Allow only one instance of any MDI child form in your MDI application

I should also say...the reason that I even said anything is because my method is quite a bit less code. Anyway...not that it matters. :-) I'm going to pull your code into a project and play with it.
Wednesday, August 16, 2006 12:55 PM by oliver

# re: Problem & Solution: Allow only one instance of any MDI child form in your MDI application

Why not use the MdiChildren from the MDI parent instead of creating and keeping a separate list. private bool IsFormAlreadyLoaded(string formToLoadName) { foreach (Form frmChild in this.MdiChildren) { if (frmChild.Name == formToLoadName) { frmChild.Activate(); return true; } } return false; }
Tuesday, October 03, 2006 5:26 AM by Ace

# re: Problem & Solution: Allow only one instance of any MDI child form in your MDI application

agree with oliver :)