MDI Question

  • Thread starter Thread starter Darin
  • Start date Start date
D

Darin

Hi,

I am creating a MDI app that will be different from the typical mdi app (MS
Word...) The parent form will be the container for 5 different child forms
that can be only created once (intialized). I have noticed that you can
create many instances of each child form. I do not want this functionality.
I was wondering what is the best way to not allow multiple instances of each
child form. I understand I could create a collection that could keep track
and code it to look for that form in the collection. However, I thought
maybe I could use a singleton approach to each child form. Is this
possible? Any better ideas?

Thanks in advance
 
Hello,

Darin said:
I am creating a MDI app that will be different from the
typical mdi app (MS Word...) The parent form will be the
container for 5 different child forms that can be only created
once (intialized). I have noticed that you can create many
instances of each child form. I do not want this functionality.
I was wondering what is the best way to not allow multiple
instances of each child form.

http://groups.google.de/[email protected]
 
Darin,
However, I thought
maybe I could use a singleton approach to each child form. Is this
possible? Any better ideas?

I would use the Singleton Pattern for each child, with a handler for closing
the form.

Something like:

Public Class MdiChildForm
Inherits Form

#Region " Singleton Pattern support "

Private Shared m_instance as MdiChildForm

Public Shared Readonly Property Instance() As MdiChildForm
Get
If m_instance Is Nothing Then
m_instance = New MdiChildForm()
End If
Return m_instance
End Get
End Property

#End Region

Private Sub New()
End Sub

Protected Overrides Sub OnClosed(ByVal e As EventArgs)
MyBase.OnClosed()
m_instance = Nothing
End Sub

End Class

This way the form is created when you first time you refer to the Instance
property, if you close the form the instance field is cleared, forcing the
form to be recreated if you refer to it again.

Then I would use MidChildForm.Instance every place else I needed to refer to
this form.

Alternatively you can handle the Closing event and prevent the form from
being closed (hide it instead). Not sure what effect this will have on
application shut down.

Hope this helps
Jay
 
You could just check the built-in collection of child form of the MDI parent
to see if the child already exist.

E.g. (assuming the child form is named "MyChildForm").

MyChildForm childForm = null;

foreach (Form frm in this.MdiChildren)
{
if (frm is MyChildForm)
{
// found it
childForm = (MyChildForm)frm;
break;
}
}

if (childForm != null)
{
childForm.Show();
childForm.Focus();
}
else
{
// Child form did not alreay exist so let's create it.
childForm = new MyChildForm();
childForm.MdiParent = this;
// You can add event handlers here
//childForm.MyEvents += new MyEventHandler(this.MyEvent);
childForm.Show();
childForm.Focus();
}

if (childForm.WindowState == FormWindowState.Minimized)
{
childForm.WindowState = FormWindowState.Normal;
}

/Magnus
 
Thanks for all the replies. I am wondering which is the better approach the
singleton design or just checking the child forms collection?

Thanks
 
Darin,
Neither, both! ;-)

I would use the Singleton Design as I tend to use OOP practices. Also the
Singleton design prevents more than one instance of the form being created
period. (Only the class can circumvent a Singleton).

Where as checking the MDI collection you would need to be certain to always
check the MDI collection when creating children, or have a specific routine
of the Parent that created the children. Which is a Factory Method Pattern
(see, I tend toward OOP ;-)) Unfortunately this routine could be
circumvented.

Do you want to empower the child? (Singleton)

Or do you want to empower the parent? (check mdi collection).

How paranoid are you about children getting created when they should not?

IMO Which is actually better really depends on your design, and the
knowledge level of your team.

Hope this helps
Jay
 
Do you have an example in C# code?

Thanks

Jay B. Harlow said:
Darin,

I would use the Singleton Pattern for each child, with a handler for closing
the form.

Something like:

Public Class MdiChildForm
Inherits Form

#Region " Singleton Pattern support "

Private Shared m_instance as MdiChildForm

Public Shared Readonly Property Instance() As MdiChildForm
Get
If m_instance Is Nothing Then
m_instance = New MdiChildForm()
End If
Return m_instance
End Get
End Property

#End Region

Private Sub New()
End Sub

Protected Overrides Sub OnClosed(ByVal e As EventArgs)
MyBase.OnClosed()
m_instance = Nothing
End Sub

End Class

This way the form is created when you first time you refer to the Instance
property, if you close the form the instance field is cleared, forcing the
form to be recreated if you refer to it again.

Then I would use MidChildForm.Instance every place else I needed to refer to
this form.

Alternatively you can handle the Closing event and prevent the form from
being closed (hide it instead). Not sure what effect this will have on
application shut down.

Hope this helps
Jay
 
Thanks, I figured it out - I was being lazy.

Jay B. Harlow said:
Darin,
I rarely use C#, its 'easy' enough to translate between the two. I usually
have problems with the property syntax, luckily VS.NET I can use the Class
View to add property methods.

Not promising anything, if I get time later I will translate it. The
following section of MSDN identifies the differences between the two
languages.

http://msdn.microsoft.com/library/d...us/vsintro7/html/vxgrfLanguageEquivalents.asp

Hope this helps
Jay
 
Back
Top