The Parent property will give you the parent Control of another control. The parent is either another control or the form. The TopLevelControl can be used to get the highest grand-parent in the hierarchy (normally the parent form).
Thanks. I noticed that the return type of the Parent property is a System::Windows::Forms::Control type. If the parent in my case is a form, then how do I use it on a text control in my app I'm a little confused about the usage.
Correct. The Parent property will return the parent of the control. If the control resides directly on the form then the parent is the form. However if the control resides in a Panel or some other control then that is the one that is returned. You should use the TopLevelControl property to get the owning form. It basically walks the parent chain until it finds a parent with no parent. A Form derives from Control so you should do a type-safe cast to Form.
how to get the parent form of a control???
FreshByte
Filipo
Chuck Crisler
I wanted ask one more question with this.
How to invoke one of the methods(or functions) with in parent from the child form.
For example, the parent is MDIForm and has child form.
From the child, if I click one button, some menus in the MDIParent must be disabled and some should be enabled.
Basically, depending the button/tab/control I use in childform, i need to enable or disable parent menu.
Since this is related to the original question, can i get some idea or sample code how to do it
Thanks,
truittjl
Control^ myControl = button1->Parent;
Form^ myForm = button1->FindForm();
Vincent_Yu
The Parent property will give you the parent Control of another control. The parent is either another control or the form. The TopLevelControl can be used to get the highest grand-parent in the hierarchy (normally the parent form).
Michael Taylor - 6/21/06
kefeng
System::Windows::Forms::Control ^myp = textBox->Parent::get();
gives me a compiler errror that it cannot convert Form^ to Control ^. What am I doing wrong
Larry Robinson
Correct. The Parent property will return the parent of the control. If the control resides directly on the form then the parent is the form. However if the control resides in a Panel or some other control then that is the one that is returned. You should use the TopLevelControl property to get the owning form. It basically walks the parent chain until it finds a parent with no parent. A Form derives from Control so you should do a type-safe cast to Form.
System::Windows::Forms::Form^ myp =
dynamic_cast<System::Windows::Forms::Form^>(textBox1->TopLevelControl);Note that I used standard C++ type-casting here. Also note that you don't need to call get() as the compiler will do this for you.
Michael Taylor - 6/21/06