By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,663 Members | 2,157 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,663 IT Pros & Developers. It's quick & easy.

Open a Form Only Once in MDI application

P: n/a
Can someone help me figure out a way to open a form only once in an MDI app.

I have an MDI app that contains several forms. I use each form depending on
the type of document that the user needs to see, i.e. for documents of type
A, I use form A, for type B, form B, etc. Each document has a unique number.

I need the app to only show one document at a time, i.e. ifthe user tries to
open doc. 1 of type A, then open form A and load doc 1. If then the user
tries to open the same doc, just focus on the open form, rather than creating
a new form of type A.

I tried implementing a singleton design pattern, and it worked fine, but of
course now I can only open one form of type A, B, C etc.

Thank you for your help.
Nov 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Konstantin,

I have an app that does something like that. Here's how I do it:

In MDI Parent class, a member variable for each type of form:
private MyPictureForm m_PictureForm = null;
private MyTextEditor m_TextEditor = null;
private MySpreadsheet m_Spreadsheet = null;

and a protected property for each type of form (I fully show just the
first here):
protected MyPictureForm PictureForm
{
get { if (m_PictureForm == null)
m_PictureForm = new MyPictureForm();
return (m_PictureForm);
}
protected MyTextEditor TextEditor...
protected MySpreadsheet Spreadsheet...

The File Open handler in the main window looks like

private void onFileOpen (...)
{
if (dlgFileOpen.ShowDialog() != DialogResult.OK)
return;

string ext = Path.GetExtension(dlgFileOpen.FileName);
if (ext.ToUpper().CompareTo(".JPG") == 0)
{
PictureForm.Load(dlgFileOpen.FileName);
PictureForm.Show();
}
else if (ext.ToUpper().CompareTo(".TXT") == 0)
{
TextEditor.Load(dlgFileOpen.FileName);
TextEditor.Show();
}
else if ... (other extensions)
}

Finally, in each form class, implement Load to check if the data
specified to load is already loaded. If it is appropriate to derive all your
form classes from a single base form class, you can do this in base.Load(),
instead of each seperate form class:

public void Load (string aFilename)
{
if (aFileName.CompareTo(m_FileName) == 0)
return; // or throw exception, log message, whatever

m_FileName = aFileName; // next time, we'll know this file is loaded

// load the data
}

This way of implementing it doesn't take advantage of a singleton pattern.
That is, the MDI parent creates only one of each form, whether that form
class enforces singleton semantics or not. That was an appropriate choice
for my app, since most of my MDI children are like yours, i.e., one of a
kind, but I have one form class that can be instantiated multiple times.

In your MDI parent form class, always use the protected property to get
the instance of the child window class; don't refer to the member variable
(except in the property itself). That way, you're guaranteed to have it be a
non-null instance.

Good luck,

PC

"Konstantin" wrote:
Can someone help me figure out a way to open a form only once in an MDI app.

I have an MDI app that contains several forms. I use each form depending on
the type of document that the user needs to see, i.e. for documents of type
A, I use form A, for type B, form B, etc. Each document has a unique number.

I need the app to only show one document at a time, i.e. ifthe user tries to
open doc. 1 of type A, then open form A and load doc 1. If then the user
tries to open the same doc, just focus on the open form, rather than creating
a new form of type A.

I tried implementing a singleton design pattern, and it worked fine, but of
course now I can only open one form of type A, B, C etc.

Thank you for your help.

Nov 22 '05 #2

P: n/a
This was the default action in VB6. The best way I've found to do it in
VB.NET is to write a very small VB6 program, convert it to VB.NET and then
examine the resulting code.

"Konstantin" wrote:
Can someone help me figure out a way to open a form only once in an MDI app.

I have an MDI app that contains several forms. I use each form depending on
the type of document that the user needs to see, i.e. for documents of type
A, I use form A, for type B, form B, etc. Each document has a unique number.

I need the app to only show one document at a time, i.e. ifthe user tries to
open doc. 1 of type A, then open form A and load doc 1. If then the user
tries to open the same doc, just focus on the open form, rather than creating
a new form of type A.

I tried implementing a singleton design pattern, and it worked fine, but of
course now I can only open one form of type A, B, C etc.

Thank you for your help.

Nov 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.