Help! I can't find my child

  • Thread starter Thread starter Jon
  • Start date Start date
J

Jon

I am writing an MDI app that uses a document manager class
to keep track of opened child windows. I want the user to
be able to close a child window, but then re-open the
window from the "Window" menu if they want.

What happens to the child window after it is closed? Even
though my document manager maintains the instance of the
child and displays the name in the menu, when I try to use
the show() method or the activate() method on a closed
child, nothing happens.

Can anyone help me with this?

Some of the code is below:

In the load event handler for the child, I register the
child with the following code-

DocumentManager.Current.RegisterDocumentView(this);


Here is the DocumentManager Class:

public class DocumentManager
{
public DocumentManager()
{}

private static DocumentManager current;
private ArrayList documents = new ArrayList();

public static DocumentManager Current
{get{return current;}}


public ArrayList Documents
{get{return documents;}}

[STAThread()] static void Main()
{
current = new DocumentManager();
///instantiate the parent window
PunchDocumentView view = new PunchDocumentView();
view.Show();
Application.Run();

public void RegisterDocumentView (frmChild view)
{
Documents.Add(view);
}
}

The popup event handler for the parent window creates the
menu with all documents currently in the manager:

private void menuWindow_Popup(object sender,
System.EventArgs e)
{
menuWindow.MenuItems.Clear();
int ordinal = 1;
foreach (frmChild view in
DocumentManager.Current.Documents)
{
WindowMenuItem item = new WindowMenuItem();
menuWindow.Menuitems.Add(item);
ordinal++;

And finally the WindowMenuItem class constructs the items
to be displayed in the menu. The overridden OnClick event
handler is where I am trying to display the closed form,
but it isn't working:

public class WindowMenuItem : System.Windows.Forms.MenuItem
{
public frmChild View;

public WindowMenuItem(int index, frmChild view)
{
View = view;
Text = string.Format("{0}{1}", index, View.Text)
}

protected override void OnClick(System.EventArgs e)
{
View.Show(); ///These 2 methods don't appear
View.Activate(); ///to do anything if the child
///is closed
}

}
 
Just thinking out loud here...

Trap the closing event.hide the window and cancel the close.

Hmm that might have implications for when you really want to close.

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm



Jon said:
Bob,

Thanks for your input, but how can I prevent the user from
closing the window?

Thanks again for your help.

Jon

-----Original Message-----
Once a child window is closed it's disposed of. If you maintain a reference
to it it will just prevent the garbage collector from finalizing it.

If you want to re-show an MDI child window, or indeed any form, don't close
it, hide it

--
Bob Powell [MVP]
C#, System.Drawing

September's edition of Well Formed is now available.
http://www.bobpowell.net/currentissue.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm



Jon said:
I am writing an MDI app that uses a document manager class
to keep track of opened child windows. I want the user to
be able to close a child window, but then re-open the
window from the "Window" menu if they want.

What happens to the child window after it is closed? Even
though my document manager maintains the instance of the
child and displays the name in the menu, when I try to use
the show() method or the activate() method on a closed
child, nothing happens.

Can anyone help me with this?

Some of the code is below:

In the load event handler for the child, I register the
child with the following code-

DocumentManager.Current.RegisterDocumentView(this);


Here is the DocumentManager Class:

public class DocumentManager
{
public DocumentManager()
{}

private static DocumentManager current;
private ArrayList documents = new ArrayList();

public static DocumentManager Current
{get{return current;}}


public ArrayList Documents
{get{return documents;}}

[STAThread()] static void Main()
{
current = new DocumentManager();
///instantiate the parent window
PunchDocumentView view = new PunchDocumentView ();
view.Show();
Application.Run();

public void RegisterDocumentView (frmChild view)
{
Documents.Add(view);
}
}

The popup event handler for the parent window creates the
menu with all documents currently in the manager:

private void menuWindow_Popup(object sender,
System.EventArgs e)
{
menuWindow.MenuItems.Clear();
int ordinal = 1;
foreach (frmChild view in
DocumentManager.Current.Documents)
{
WindowMenuItem item = new WindowMenuItem();
menuWindow.Menuitems.Add(item);
ordinal++;

And finally the WindowMenuItem class constructs the items
to be displayed in the menu. The overridden OnClick event
handler is where I am trying to display the closed form,
but it isn't working:

public class WindowMenuItem : System.Windows.Forms.MenuItem
{
public frmChild View;

public WindowMenuItem(int index, frmChild view)
{
View = view;
Text = string.Format("{0}{1}", index, View.Text)
}

protected override void OnClick(System.EventArgs e)
{
View.Show(); ///These 2 methods don't appear
View.Activate(); ///to do anything if the child
///is closed
}

}


.
 
Back
Top