26

I Create a Windows Forms application with C#.

I have a general Form and a panel on it.

I show subForm into this panel with code:

SubForm objForm= SubForm.InstanceForm();
this.IsMdiContainer = true;
objForm.TopLevel = false;
pnlSubSystem.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();

now I want to show other form on subForm of this panel, But I dont know how to do it.

Mohit S
  • 13,723
  • 6
  • 34
  • 69
Iraj
  • 1,492
  • 5
  • 18
  • 42
  • 3
    There is no point in setting IsMdiContainer to true when you do this. If you want another form to be visible in that panel then it is up to you to set the Size and Location properties. You don't get the MDI behavior where forms can overlap. – Hans Passant Jul 31 '13 at 06:53

5 Answers5

36

I think your problem resolved by this code:

    SubForm objForm= SubForm.InstanceForm();
    objForm.TopLevel = false;
    pnlSubSystem.Controls.Add(objForm);
    objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    objForm.Dock = DockStyle.Fill;
    objForm.Show();
M.Mohammadi
  • 1,558
  • 1
  • 13
  • 25
4

As I understand, you're very close. To add another form into subform try the same code instead:

pnlSubSystem.Controls.Add(objForm);

use (where objForm2 is the new subForm)

SubForm objForm2 = new SubForm();
objForm.Controls.Add(objForm2); 
Andrii Kalytiiuk
  • 1,501
  • 14
  • 26
Martijn van Put
  • 3,293
  • 18
  • 17
3

Since you already got the answer that by removing this.IsMdiContainer = true; your code would run perfectly fine. Because IsMdiContainer property changes the display and behavior of the form to an MDI parent form. When this property is set to true, the form displays a submerged client area. All MDI child forms assigned to the parent form are displayed within its client area.

SubForm objForm= SubForm.InstanceForm();
objForm.TopLevel = false;
pnlSubSystem.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();

objForm form which will be the template for the child forms. Each time you want to create a new child window to your application, you can create a new instance of this template form and make the first form as its parent form.

//Create a new instance of the MDI child template form
SubForm objForm = new SubForm(); 
//Set parent form for the child window 
objForm.MdiParent=this; // Last ObjForm or something
//Display the child window
objForm.Show();
Mohit S
  • 13,723
  • 6
  • 34
  • 69
2

Another way:

objForm.TopLevel = false;
objForm.Parent = pnlSubSystem;
objForm.Show();

This is my first answer on Stackoverflow.

0

Blockquote Languege Vietnamese :))

public void Xe_ExamSchedulerMethod()
{
    if (xeForm == null)
    {
        xeForm = new Form_Xe();
        xeForm.TopLevel = false;
        xeForm.Dock = DockStyle.Fill;

        // Thêm form_Xe vào control con cuối cùng trong Xe_tableLayoutPanel2
        Xe_tableLayoutPanel2.Controls.Add(xeForm);

        // Đăng ký sự kiện Closed để set xeForm về null
        xeForm.Closed += (s, args) => { xeForm = null; };
    }

    // Hiển thị form_Xe
    xeForm.Show();
}
Dev dEV
  • 1
  • 1
  • 1
    Please avoid code-only answer and explain why and how it is likely to solve the problem. – XouDo May 10 '23 at 12:08