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.
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.
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.
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?
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;
}
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.
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.
Re: Accessing current Mdi child
Ok, Thank you for your help
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.
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.
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
Re: Accessing current Mdi child
In the child class you should use this.MdiParent.ActiveMdiChild as mentioned in your previous post.
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
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
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