开发者

working with three winforms as mdi parent and child

I have 3 forms-Form1,2,3. Form1 is MdiContainer. Now, when a 开发者_Go百科button on Form1 is clicked it shows Form2, and on Form2 when a button is clicked then it shows Form3. my code is as below but it gives error that Form2 is not MdiContainer. but if i make Form2 as MdiContainer then it gives error that Form2 is a MdiChild and can't be MdiContainer.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();
        f2.MdiParent = this;
    }
}

public partial class Form2 : Form

{
    public Form2()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {

        Form3 f3 = new Form3();
        f3.Show();
        f3.MdiParent = this;
    }
}

how to do this?

In short , i want that : Parent of Form2 is Form1, and, Parent of Form3 is Form2


You can't. You can only have one MDI parent. Within that parent container you can have many child forms, but those forms can only spawn other forms directly and not as MDI children.

In Microsoft's own words:

The Microsoft Windows implementation of the multiple document interface (MDI) does not support nested MDI client windows. In other words, neither an MDI client window nor an MDI child window can have an MDI client window as a child.


It won't work because Form2 is not an MDI container.


Basically this functionality is not supported, however you may be able to replicate it in other ways. Is it simply for visual representation that you want f2 to be the parent of f3? I am trying to understand in order to give you a proper answer. So what do you want to accomplish? If you want f3 to be embeded on f2, you can create a user control and add it as a control to your form.

If you want f3 to popup you could use

f3.showdialog();

If you do this don't specify the mdiparent of f3.

Eric


I tried by writing as follows with out

 f2.MdiParent = this;

The code works

If you want to do with out showdialog means write the following on Button_click

bool IsOpen = false;
foreach (Form f in Application.OpenForms)
{
    if (f.Text == "Form2") //  Name of the Form
    {
        IsOpen = true;
        f.Focus();
        break;
    }
}
if (IsOpen == false)
{
    Form2 f2 = new Form2();
   //f2.MdiParent = this;
    f2.Show();
}


There is only one MdiContainer in an application and others should be child forms.

However you can approximately achieve your functionality by doing ShowDialog instead of Show()

private void button1_Click(object sender, EventArgs e)
{
    Form3 f3 = new Form3();
    f3.ShowDialog();
}

Edit 1 Due to your comments, Solution provided by @Dorababu is best for you with some improvements; made form1 as parent of form3. So your form3 will be displayd inside MdiParent.

private void button1_Click(object sender, EventArgs e)
{
    Form3 f3 = new Form3();
   //f3.MdiParent = this;
    // instead use this.MdiParent
    f3.MdiParent = this.MdiParent;
    f2.Show();
}

And check on get focus event of form2 if form3 is displayed then set focus to form3, so form2 will not get focused until form3 is opened.

private void Form2_Activated(object sender, EventArgs e)
{
    // inside Focus event of Form2
    foreach (Form f in Application.OpenForms)
    {
        if (f.Text == "Form3") //  Name of the Form
        {
            f.Focus();
            break;
        }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜