how to get the parent form of a control???

Can anybody tell me how to use the "parent::get()" property of a text control in order to obtain its parent form

Thanks!!



Answer this question

how to get the parent form of a control???

  • FreshByte

    It looks like you will have to traverse the parent's parent until the return type is System::Windows::Forms::Form.
  • Filipo

    Excellent. Thanks you guys. That worked like a charm!!
  • Chuck Crisler

    Dave S. Anderson wrote:
    Use this:

    Control^ myControl = button1->Parent;
    Form^ myForm = button1->FindForm();



    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

    Use this:

    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

    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.

    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


  • how to get the parent form of a control???