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

Re: MDI Question by Jay

Jay
Wed Sep 10 17:17:08 CDT 2003

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

"Darin" <test@yahoo.com> wrote in message
news:uSX8Mc%23dDHA.1836@TK2MSFTNGP09.phx.gbl...
> 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
>
>



Re: MDI Question by Herfried

Herfried
Wed Sep 10 17:16:08 CDT 2003

Hello,

"Darin" <test@yahoo.com> schrieb:
> 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/groups?selm=u5iBdcHRDHA.1552%40TK2MSFTNGP10.phx.gbl

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet



Re: MDI Question by Magnus

Magnus
Thu Sep 11 01:31:10 CDT 2003

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

"Darin" <test@yahoo.com> wrote in message
news:uSX8Mc%23dDHA.1836@TK2MSFTNGP09.phx.gbl...
> 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
>
>



Re: MDI Question by Darin

Darin
Thu Sep 11 10:29:52 CDT 2003

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" <test@yahoo.com> wrote in message
news:uSX8Mc%23dDHA.1836@TK2MSFTNGP09.phx.gbl...
> 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
>
>



Re: MDI Question by Jay

Jay
Thu Sep 11 11:29:34 CDT 2003

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

"Darin" <test@yahoo.com> wrote in message
news:OviX9mHeDHA.3820@tk2msftngp13.phx.gbl...
> 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" <test@yahoo.com> wrote in message
> news:uSX8Mc%23dDHA.1836@TK2MSFTNGP09.phx.gbl...
> > 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
> >
> >
>
>



Re: MDI Question by Darin

Darin
Thu Sep 11 14:58:39 CDT 2003

Do you have an example in C# code?

Thanks

"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@email.msn.com> wrote in message
news:%23X1NIl%23dDHA.1712@TK2MSFTNGP09.phx.gbl...
> 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
>
> "Darin" <test@yahoo.com> wrote in message
> news:uSX8Mc%23dDHA.1836@TK2MSFTNGP09.phx.gbl...
> > 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
> >
> >
>
>



Re: MDI Question by Jay

Jay
Thu Sep 11 16:32:57 CDT 2003

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/default.asp?url=/library/en-us/vsintro7/html/vxgrfLanguageEquivalents.asp

Hope this helps
Jay

"Darin" <test@yahoo.com> wrote in message
news:%23mGsJ9JeDHA.392@TK2MSFTNGP12.phx.gbl...
> Do you have an example in C# code?
>
> Thanks
>
> "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@email.msn.com> wrote in
message
> news:%23X1NIl%23dDHA.1712@TK2MSFTNGP09.phx.gbl...
> > 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
> >
> > "Darin" <test@yahoo.com> wrote in message
> > news:uSX8Mc%23dDHA.1836@TK2MSFTNGP09.phx.gbl...
> > > 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
> > >
> > >
> >
> >
>
>



Re: MDI Question by Darin

Darin
Thu Sep 11 16:39:53 CDT 2003

Thanks, I figured it out - I was being lazy.

"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@email.msn.com> wrote in message
news:eqnaGxKeDHA.3096@TK2MSFTNGP11.phx.gbl...
> 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/default.asp?url=/library/en-us/vsintro7/html/vxgrfLanguageEquivalents.asp
>
> Hope this helps
> Jay
>
> "Darin" <test@yahoo.com> wrote in message
> news:%23mGsJ9JeDHA.392@TK2MSFTNGP12.phx.gbl...
> > Do you have an example in C# code?
> >
> > Thanks
> >
> > "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow@email.msn.com> wrote in
> message
> > news:%23X1NIl%23dDHA.1712@TK2MSFTNGP09.phx.gbl...
> > > 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
> > >
> > > "Darin" <test@yahoo.com> wrote in message
> > > news:uSX8Mc%23dDHA.1836@TK2MSFTNGP09.phx.gbl...
> > > > 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
> > > >
> > > >
> > >
> > >
> >
> >
>
>