-1

I am trying to insert a form inside another form. I used .Controls.Add(form) and it works. My problem is that I have to declare the owner and to do that I have to set the toplevel to true but if I set the toplevel to true I get stuck with the control.add bacause telling me that it is impossible to add a top level control to a control.

How can I do?

 public void openChildForm(Form childForm)
    {
        if (activeForm != null) activeForm.Close();
        activeForm = childForm;
        childForm.TopLevel = true;
        childForm.TopMost = true;


        childForm.FormBorderStyle = FormBorderStyle.None;
        childForm.Dock = DockStyle.Fill;
        
        this.Controls.Add(childForm);
        panelChildForm.Tag = childForm;

        childForm.Owner = this;
        childForm.BringToFront();
        childForm.Show();




    }

thankyou in advance, Davide.

  • You can add a form as a child in another control when yout set childForm.TopLevel to false. Why do you need to set the owner? (Another possibility would be using an UserControl but there no owner which can be set) – marsh-wiggle Aug 18 '20 at 13:15
  • Controls.Add() can only work with TopLevel = false. The effective "owner" of such a control now is `Parent`. If that form requires Owner to be set to operate correctly then you cannot use it this way. – Hans Passant Aug 18 '20 at 14:46
  • Did any of the answers help? – CobyC Aug 19 '20 at 11:12
  • I need Owner property. That's is the problem. I have to put it bacause I have to be able to disable and renable button in one form from another one – DavidSpecter Aug 19 '20 at 12:32
  • You should add that to your question so that it makes it easier to understand why you are trying to do this. if you want to access other forms within the same application you can use `Application.OpenForms["form1"]` or `Application.OpenForms[0]` if you use MdiForms you can use `parentMdiForm.MdiChildren[0]` – CobyC Aug 19 '20 at 13:46

3 Answers3

0

For me it's a bit strange what you are trying to do. A form is kind of window. Why would you add a window in another window? Don't you want to create a user control and to display that user control in your existing form?

Lung Radu
  • 15
  • 3
0

It looks like you are working with WinForms.

Look at MDI Forms (Multiple-Document Interface Forms)

You can handle this by setting the parent form property IsMdiContainer = true and then the child form childForm.MdiParent = parentForm;.

Here is a small example:

Add a menuStrip to the parentForm and set the parent form MainMenuStrip = menuStrip. Add a menu item and add some code to the menuStrip_ItemClicked event.

private void menuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
    if (e.ClickedItem.Text == "New Child")
        ShowNewForm(sender, e);
} 

private void ShowNewForm(object sender, EventArgs e)
{
    Form childForm = new Form();
    childForm.MdiParent = this;
    childForm.Text = "Window " + childFormNumber++;
    childForm.Show();
}

You should end up with this behaviour. enter image description here

If you want to access other forms within the same application you can use a loop and find a form using Application.OpenForms or find a form using either Application.OpenForms["form1"] or Application.OpenForms[0]

If you use MdiForms you can find child forms using parentMdiForm.MdiChildren

CobyC
  • 2,058
  • 1
  • 18
  • 24
0

There isn't enough information to go on from the original question you've posted, but it looks like you're trying to create the kind of functionality that a sidebar page menu might offer. In that situation, clicking on a button on the sidebar changes the window that's currently being displayed. Perhaps this question about sidebar navigation would relate to what you're saying? There's some information also available if you try searching "winforms sidebar navigation," such as this post about creating navigation in winforms, and there are a lot more linked from this question.

I'm just taking a guess at what you're trying to do here. Perhaps if you want to refine the answers you're getting, the original question should include a link to a github sample project, or at least a bit more of an explanation of what the target is.

Nate W
  • 275
  • 2
  • 13