CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jul 2006
    Posts
    3

    Question HELP: can you trap a Forms closed() event??

    I have an MDI application and when I click a menu it loads a form, but if you click the menu again it loads the same form again. It might corrupt my memory so I need to only allow one instance of the form. If the form is already loaded I just want it to BrintToFront() the form. I have tried several ways but nothing seems to work aside from using a global variable which I don't like to do.

    Is there a way to trap the forms Closed() event so I can tell when it was closed, or is there a way to use a reference counter? Does someone have a code example of how to do this?

    Thanks.

    P.S. I don't want to do the hack that disables the menu item when the form is loaded, I don't like that. I want the user to still be able to click the menu item, and BringToFront() the form.

    Basically, I need to know how to have the mdi child form tell its parent form when its been unloaded.

    Thanks!

  2. #2
    Join Date
    Jul 2006
    Posts
    59

    Re: HELP: can you trap a Forms closed() event??

    Instead of making the form variable global, why don't you make it a private member of the mdiparent and then add a function like LoadChildForm to which you could pass the type of the form you are checking if it exists? The function could check your member variable to see if it's null or if it has been disposed, and if so, then it would allocate a new form. If it does not need to be allocated, you could simply call BringToFront on the member variable.

    Something like below...
    PHP Code:
    void LoadChildForm(Type tp)
    {
        switch(
    tp.ToString())
    {
            case 
    "AboutForm":
            if(
    this.m_frmAbout == null || this.m_frmAbout.IsDisposed)
                 
    this.m_frmAbout = new AboutForm();
            
    this.m_frmABout.BringToFront();
            break;
    }

    If my posts have helped you, please rate them. Ratings give me that warm, fuzzy feeling of having helped someone out

  3. #3
    Join Date
    Oct 2005
    Location
    Islamabad, Pakistan
    Posts
    1,277

    Re: HELP: can you trap a Forms closed() event??

    function checks whether new form object is a part of mdiChildern collection ,if it is the don't create new instance of the form.
    Code:
    void chk(Form frm) 
    { 
     Form f; 
     foreach (int f in this.MdiChildren) { 
       if (((Form)(f)).Name == frm.Name) { 
         frm.Close(); 
    return; 
       } 
     } 
     frm.MdiParent = this; 
     frm.Show(); 
    }

  4. #4
    Join Date
    Jul 2006
    Posts
    3

    Re: HELP: can you trap a Forms closed() event??

    I cant get either one to work. The first one doesn't do anything when I click the menu item. I don't know.

  5. #5
    Join Date
    Jul 2006
    Posts
    59

    Re: HELP: can you trap a Forms closed() event??

    Well, why dont you show us then what you have tried that it does not work? It's difficult to help when we don't see your code.
    If my posts have helped you, please rate them. Ratings give me that warm, fuzzy feeling of having helped someone out

  6. #6
    Join Date
    Mar 2004
    Location
    33°11'18.10"N 96°45'20.28"W
    Posts
    1,808

    Re: HELP: can you trap a Forms closed() event??

    in your mdi parent, you have to initialize the child window. you have a collection of MDI Children that you can check before creating the new window. if it already exists, than you can bring it to the front.

    there is a Closing event on the form that you can attach to as well.

  7. #7
    Join Date
    Aug 2006
    Posts
    2

    Re: HELP: can you trap a Forms closed() event??

    hi

    You can easily trap the form close event.
    For this you have to write your own event handler for form close event and in that event handler you can do what ever you want.

    int the code form of which you want to trap the close event write following

    In the event declearation block where the events are decleared for button and for every control write this line

    this.Closing += new System.ComponentModel.CancelEventHandler(this.DoSomething);

    By declearing this you are calling DoSomething method when you close the form now define the method.


    private void DoSomething(object sender, System.ComponentModel.CancelEventArgs e)
    {
    // now you can continue doing what you want
    }

    here you can keep some flag which will be set or reset when form close.

  8. #8
    Join Date
    Oct 2001
    Posts
    80

    Re: HELP: can you trap a Forms closed() event??

    Why don't you go for the simple solution? Just add for each child form a boolean in de MDI parent. Then when the user clicks the menu item or whatever you check for that boolean you just code for example:

    if ( m_bChildForm1IsOpened )
    {
    //do code to maximize ChildForm1
    }
    else
    {
    //do code for opening the form.
    bChildForm1IsOpened = true;
    }

    edit:
    Having reread my post, I realise how quick and dirty this is. However, I have used this before and it works great.
    Last edited by bewa; August 8th, 2006 at 05:58 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured