Help! I can't find my child

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon

    Help! I can't find my child

    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.Regist erDocumentView( 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
    PunchDocumentVi ew view = new PunchDocumentVi ew();
    view.Show();
    Application.Run ();

    public void RegisterDocumen tView (frmChild view)
    {
    Documents.Add(v iew);
    }
    }

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

    private void menuWindow_Popu p(object sender,
    System.EventArg s e)
    {
    menuWindow.Menu Items.Clear();
    int ordinal = 1;
    foreach (frmChild view in
    DocumentManager .Current.Docume nts)
    {
    WindowMenuItem item = new WindowMenuItem( );
    menuWindow.Menu items.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
    }

    }

  • Bob Powell [MVP]

    #2
    Re: Help! I can't find my child

    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.


    Answer those GDI+ questions with the GDI+ FAQ




    "Jon" <jhirsch3@netze ro.net> wrote in message
    news:0b0301c38c 6a$37154a30$a10 1280a@phx.gbl.. .[color=blue]
    > 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.Regist erDocumentView( 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
    > PunchDocumentVi ew view = new PunchDocumentVi ew();
    > view.Show();
    > Application.Run ();
    >
    > public void RegisterDocumen tView (frmChild view)
    > {
    > Documents.Add(v iew);
    > }
    > }
    >
    > The popup event handler for the parent window creates the
    > menu with all documents currently in the manager:
    >
    > private void menuWindow_Popu p(object sender,
    > System.EventArg s e)
    > {
    > menuWindow.Menu Items.Clear();
    > int ordinal = 1;
    > foreach (frmChild view in
    > DocumentManager .Current.Docume nts)
    > {
    > WindowMenuItem item = new WindowMenuItem( );
    > menuWindow.Menu items.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
    > }
    >
    > }
    >[/color]


    Comment

    • Bob Powell [MVP]

      #3
      Re: Help! I can't find my child

      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.


      Answer those GDI+ questions with the GDI+ FAQ




      "Jon" <jhirsch3@netze ro.net> wrote in message
      news:09f801c38c e7$4c4748e0$a00 1280a@phx.gbl.. .[color=blue]
      > Bob,
      >
      > Thanks for your input, but how can I prevent the user from
      > closing the window?
      >
      > Thanks again for your help.
      >
      > Jon
      >
      >[color=green]
      > >-----Original Message-----
      > >Once a child window is closed it's disposed of. If you[/color]
      > maintain a reference[color=green]
      > >to it it will just prevent the garbage collector from[/color]
      > finalizing it.[color=green]
      > >
      > >If you want to re-show an MDI child window, or indeed any[/color]
      > form, don't close[color=green]
      > >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" <jhirsch3@netze ro.net> wrote in message
      > >news:0b0301c38 c6a$37154a30$a1 01280a@phx.gbl. ..[color=darkred]
      > >> I am writing an MDI app that uses a document manager[/color][/color]
      > class[color=green][color=darkred]
      > >> to keep track of opened child windows. I want the user[/color][/color]
      > to[color=green][color=darkred]
      > >> 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?[/color][/color]
      > Even[color=green][color=darkred]
      > >> though my document manager maintains the instance of the
      > >> child and displays the name in the menu, when I try to[/color][/color]
      > use[color=green][color=darkred]
      > >> 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.Regist erDocumentView( 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
      > >> PunchDocumentVi ew view = new PunchDocumentVi ew[/color][/color]
      > ();[color=green][color=darkred]
      > >> view.Show();
      > >> Application.Run ();
      > >>
      > >> public void RegisterDocumen tView (frmChild view)
      > >> {
      > >> Documents.Add(v iew);
      > >> }
      > >> }
      > >>
      > >> The popup event handler for the parent window creates[/color][/color]
      > the[color=green][color=darkred]
      > >> menu with all documents currently in the manager:
      > >>
      > >> private void menuWindow_Popu p(object sender,
      > >> System.EventArg s e)
      > >> {
      > >> menuWindow.Menu Items.Clear();
      > >> int ordinal = 1;
      > >> foreach (frmChild view in
      > >> DocumentManager .Current.Docume nts)
      > >> {
      > >> WindowMenuItem item = new WindowMenuItem( );
      > >> menuWindow.Menu items.Add(item) ;
      > >> ordinal++;
      > >>
      > >> And finally the WindowMenuItem class constructs the[/color][/color]
      > items[color=green][color=darkred]
      > >> to be displayed in the menu. The overridden OnClick[/color][/color]
      > event[color=green][color=darkred]
      > >> handler is where I am trying to display the closed form,
      > >> but it isn't working:
      > >>
      > >> public class WindowMenuItem :[/color][/color]
      > System.Windows. Forms.MenuItem[color=green][color=darkred]
      > >> {
      > >> 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[/color][/color]
      > appear[color=green][color=darkred]
      > >> View.Activate() ; ///to do anything if the[/color][/color]
      > child[color=green][color=darkred]
      > >> ///is closed
      > >> }
      > >>
      > >> }
      > >>[/color]
      > >
      > >
      > >.
      > >[/color][/color]


      Comment

      Working...