Getting the form that hosts a control

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mystery Man

    Getting the form that hosts a control

    I have developed a series of user defined controls. These have been
    placed on a variety of different forms.

    From within one of these controls, I want to show another dialog
    directly to the right of the current form (that contains one of this
    particular control).

    Therefore, what I am after is the position of the form that hosts a
    control from the control itself. I realise that I can do the
    following:

    this.Parent.Par ent...Location;

    However, as this control is on a variety of forms, there is no
    guarantee how many parents objects I need to retrieve in order to get
    to the parent form.

    Is there a way to work out which parent is the form itself or is there
    a better way of doing this?
  • Aravind C

    #2
    Re: Getting the form that hosts a control

    Hi,

    You may like to traverse the hierarchy, until you find a parent
    that is of type System.Windows. Forms.Form.

    // 'this' refers to the user control here
    Control ctrl = this;
    while(ctrl.GetT ype() != typeof(System.W indows.Forms.Fo rm))
    {
    ctrl = ctrl.Parent;
    }

    if(ctrl != this)
    {
    // Found the host form
    MyForm form = (MyForm)ctrl;

    }

    Regards,
    Aravind C


    "Mystery Man" <PromisedOyster @hotmail.com> wrote in message
    news:87c81238.0 311230438.a7c30 a5@posting.goog le.com...[color=blue]
    > I have developed a series of user defined controls. These have been
    > placed on a variety of different forms.
    >
    > From within one of these controls, I want to show another dialog
    > directly to the right of the current form (that contains one of this
    > particular control).
    >
    > Therefore, what I am after is the position of the form that hosts a
    > control from the control itself. I realise that I can do the
    > following:
    >
    > this.Parent.Par ent...Location;
    >
    > However, as this control is on a variety of forms, there is no
    > guarantee how many parents objects I need to retrieve in order to get
    > to the parent form.
    >
    > Is there a way to work out which parent is the form itself or is there
    > a better way of doing this?[/color]


    Comment

    • Chris R

      #3
      Re: Getting the form that hosts a control

      UserControl has a ParentForm property that is ideal for this situation.
      UserControl is derived from ContainerContro l, so you'll find the definition
      under "ContainerContr ol.ParentForm Property".

      Chris R.

      "Mystery Man" <PromisedOyster @hotmail.com> wrote in message
      news:87c81238.0 311230438.a7c30 a5@posting.goog le.com...[color=blue]
      > I have developed a series of user defined controls. These have been
      > placed on a variety of different forms.
      >
      > From within one of these controls, I want to show another dialog
      > directly to the right of the current form (that contains one of this
      > particular control).
      >
      > Therefore, what I am after is the position of the form that hosts a
      > control from the control itself. I realise that I can do the
      > following:
      >
      > this.Parent.Par ent...Location;
      >
      > However, as this control is on a variety of forms, there is no
      > guarantee how many parents objects I need to retrieve in order to get
      > to the parent form.
      >
      > Is there a way to work out which parent is the form itself or is there
      > a better way of doing this?[/color]


      Comment

      Working...