Parenting a form?

Hi all ~

am trying to dynamically create a form and display it "inside" a panel on my main form.
Am coming from Delphi where similar code would work - what do I need to do in
C#

TIA,
Kai

private void button1_Click(object sender, EventArgs e)
{
Form f = new Form();
f.Parent = panel2;
f.ShowDialog(this);
}



Answer this question

Parenting a form?

  • imrash

    No, just tried it but it isn't suitable at all since MDI forms have all these undesirable attributes (for my purposes) like caption bar, menu functionality, sizing issues etc.etc..

    I am using separate forms in my Delphi app. mainly for the fact that they allow me to keep code & components together as one entity in one file, i.e. everything pertaining to, say, address maintenance is in a separate source code file (two, technically speaking, .PAS for source and .DFM for the components).

    I just discovered the "partial" keyword though, which apparently allows me to break my source code down unto topical parts - thus I should be able to achieve what I want in a different way!

    Thanks to all for trying to help
    Kai


  • SriDirectX

    No, not all. I want to display various forms inside the main form to the left of an "Outlook bar".

    In my existing Delphi application I achieve this by creating forms dynamically and parenting them onto a client aligned panel (borders & caption bar turned off). There are many forms that I display that way and that's also the reason why I build them dynamically: To save resources.



  • debug13

    I'm not sure that you can house a form inside a form. The way to do this would be to create a user control that is used both by the form, and can be shown inside your form. To add a control dynamically, it needs to be added to the parent forms controls collection ( this.Controls.Add, from memory ). I still don't think this works for a Form, but it might. You also need to set the position of the control inside it's parent. Overall, it's easier to create user controls, put them all on the form in the designer, and use the visible property to show and hide them.



  • Forrestsjs

    hi,

    you might take a look at MDI(Multiple Document Interface) Forms as what Mordat said

    here its the main(parent) form

    http://msdn2.microsoft.com/en-us/library/d4dabts7.aspx

    and this is the child forms

    http://msdn2.microsoft.com/en-us/library/7aw8zc76.aspx 

    i guess its what you are talking about without adding any panel you can achieve the same result, because you can't add a single form inside a single form as what cgraus said

    hope this helps



  • abohmza

    You might want to look at Multiple Document Interface Windows. I assume this is what you are referring to.
  • Parenting a form?