CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Feb 2001
    Location
    Sweden
    Posts
    82

    Accessing current Mdi child

    Hi,

    This is probably a very simple problem, but I can't seem to solve it. I have an Mdi application and I need to access the current Mdi child window in a method. When I use Form.ActiveForm.ActiveMdiChild I get a nullReference exception because Form.ActiveForm returns an undefined value. Why is this value undefined ? I thought that Form.ActiveForm returns the main Form of the application which is created at the start of the application.

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Accessing current Mdi child

    Have you tried this.ActiveMdiChild.

    Actually you are trying to use a Class and not the currect object, thats why it gives a NullReference Exception. If you want to access the current instance of the object from the code, you should be using this.

  3. #3
    Join Date
    Feb 2001
    Location
    Sweden
    Posts
    82

    Re: Accessing current Mdi child

    this.ActiveMdiChild will only work in an instance method of a class derived from Form. I need to access the current Mdi child in an instance method of a class not derived from Form. Using Form.ActiveForm is ok because ActiveForm is a (public) static property.

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Accessing current Mdi child

    Quote Originally Posted by NetMaster
    this.ActiveMdiChild will only work in an instance method of a class derived from Form. I need to access the current Mdi child in an instance method of a class not derived from Form. Using Form.ActiveForm is ok because ActiveForm is a (public) static property.
    I just tried this. Added two Forms (one MDIParent and other MDI Child) and a class. In the Class I wrote a method which shows the Name of the current active MDIChild. It all works fine.

    Can you post the code that you are using and how are you showing the MDIChild form?

  5. #5
    Join Date
    Feb 2001
    Location
    Sweden
    Posts
    82

    Re: Accessing current Mdi child

    This instance method is a member of a class that is derived from Panel:

    Code:
    protected Size GetTextExtent(string str)
    {
        Size padding = new Size(30,16);
        Graphics gc  = Form.ActiveForm.ActiveMdiChild.CreateGraphics();
        SizeF size   = gc.MeasureString(name, this.Font);
        return size.ToSize() + padding;
    }

  6. #6
    Join Date
    Feb 2001
    Location
    Sweden
    Posts
    82

    Re: Accessing current Mdi child

    I just discovered that this.CreateGraphics() works just fine. The mdi child is created like this
    Code:
    ChildForm childForm = new ChildForm();
    childForm.MdiParent = this;
    childForm.Text      = GetNewDocName();
    childForm.Show();
    This code is executed (in the parent Form) just before calling GetTextExtent(). I still don't understand why Form.ActiveForm is undefined though.

  7. #7
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Accessing current Mdi child

    Quote Originally Posted by NetMaster
    This instance method is a member of a class that is derived from Panel:

    Code:
    protected Size GetTextExtent(string str)
    {
        Size padding = new Size(30,16);
        Graphics gc  = Form.ActiveForm.ActiveMdiChild.CreateGraphics();
        SizeF size   = gc.MeasureString(name, this.Font);
        return size.ToSize() + padding;
    }
    I would suggest that you pass the Instance of the MDIParent to the class before you use the Creategraphics method on it.

  8. #8
    Join Date
    Feb 2001
    Location
    Sweden
    Posts
    82

    Re: Accessing current Mdi child

    Ok, Thank you for your help

  9. #9
    Join Date
    Mar 2006
    Posts
    9

    Re: Accessing current Mdi child

    I am having a similar issue. The MDI children are form derived and I need to identifiy which window has the focus. I'm using this.MdiParent.ActiveMdiChild to derive the information, but what information in the object will determine this? The only thing, in my application, that seems to differentiate between the windows is the different WindowText that I assign for each child. And with that, I can't seem to access the text string as I get an error that says the information is protected. I am relatively new to Windows programming, so excuse me if I sound a bit naive.

  10. #10
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Accessing current Mdi child

    Welcome to Forum

    If You are trying to access the current Active MDIChild from the MDIForm then you can use
    Code:
    this.ActiveMDIChild.Text
    to get the Caption of the active MDIChild window.

  11. #11
    Join Date
    Mar 2006
    Posts
    9

    Re: Accessing current Mdi child

    That works fine if I access this.ActiveMdiChild from a method in the class from which the child was spawned (MainForm). When I try to access it from a click event in the child class, it provides a null reference. Should this.ActiveMdiChild be global in scope [edit] or is the problem something else?

    Thanks

    Larry
    Last edited by SimiCyclist; March 9th, 2006 at 12:39 PM. Reason: additional thought

  12. #12
    Join Date
    Jul 2002
    Location
    India
    Posts
    505

    Re: Accessing current Mdi child

    In the child class you should use this.MdiParent.ActiveMdiChild as mentioned in your previous post.

  13. #13
    Join Date
    Mar 2006
    Posts
    9

    Re: Accessing current Mdi child

    this.MdiParent.ActiveMdiChild provides a null reference as well. Here's what I get thus far:

    In the MainForm ActiveMdiChild provides a valid reference, but MdiParent.ActiveMdiChild provides a null reference.

    In the child class both provide a null reference.

    Larry

  14. #14
    Join Date
    Jul 2002
    Location
    India
    Posts
    505

    Re: Accessing current Mdi child

    Is MainForm an Mdi frame? i.e is its IsMdiContainer property set to True?

    Is the childform an mdi child i.e when you create an instance of the child form, are you setting its MdiParent property?

    Posting some code my help find the problem

    -Satish

  15. #15
    Join Date
    Mar 2006
    Posts
    9

    Re: Accessing current Mdi child

    While isolating the code to provide an example, I discovered my problem (it was actually in the wrong event, and was not defined yet). Thanks for the help.

    Larry

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