|
-
March 8th, 2006, 08:18 AM
#1
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.
-
March 8th, 2006, 08:32 AM
#2
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.
-
March 8th, 2006, 09:17 AM
#3
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.
-
March 8th, 2006, 09:39 AM
#4
Re: Accessing current Mdi child
 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?
-
March 8th, 2006, 09:54 AM
#5
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;
}
-
March 8th, 2006, 10:04 AM
#6
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.
-
March 8th, 2006, 10:05 AM
#7
Re: Accessing current Mdi child
 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.
-
March 8th, 2006, 10:34 AM
#8
Re: Accessing current Mdi child
Ok, Thank you for your help
-
March 8th, 2006, 05:25 PM
#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.
-
March 9th, 2006, 02:41 AM
#10
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.
-
March 9th, 2006, 12:27 PM
#11
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
-
March 9th, 2006, 01:05 PM
#12
Re: Accessing current Mdi child
In the child class you should use this.MdiParent.ActiveMdiChild as mentioned in your previous post.
-
March 9th, 2006, 01:17 PM
#13
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
-
March 9th, 2006, 03:13 PM
#14
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
-
March 9th, 2006, 08:01 PM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|