如何从FormB打开FormC作为FormA的MDI_Child [英] How to open FormC as an MDI_Child of FormA, from FormB

查看:67
本文介绍了如何从FormB打开FormC作为FormA的MDI_Child的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#.NET的初学者,尝试编写代码。我有三个Windows.Form:

1. MainForm(MDI_FORM)
2. Child1
3. Child2

在MainForm上,我命令Child1。显示()
然后从Child1,我打算通过以下代码分配并打开Child2作为孩子到MainForm:

在MainForm上,我声明:

public static Child2 child2;


然后在来自Child1的btnOpen上,我编码:

private void btnOpen_Click(object sender,EventArgs e)
{
MainForm mainForm = new MainForm();
mainForm.child2 = new Child2();
mainForm.child2.MdiParent = fMainForm;
mainForm.child2.Show();
}

有人帮我做点什么。
这不起作用。
非常感谢!!!

解决方案

嗨Salisu Shaibu,



您的代码无效,因为您正在创建MainForm的新实例。诀窍是使用对MainForm的引用并将其设置为MdiParent到您的第二个Form。看看这个(可运行的)示例:



 使用系统; 
使用 System.Windows.Forms;

命名空间 MdiOhMy
{
静态 class 程序
{
[STAThread]
static void Main()
{
Form formMain = new Form {IsMdiContainer = true };

表格formChild1 = new 表格{Text = Child1,MdiParent = formMain,Visible = true };
Button buttonShowChild2 = new 按钮{Text = 打开Child2,Parent = formChild1};
buttonShowChild2.Click + =( object sender,EventArgs e)= >
{
Form formChild2 = new 表格{Text = Child2,MdiParent = formMain,Visible = true };
};

Application.Run(formMain);
}
}
}





在这个例子中,我可以引用formMain实例,因为它在捕获的范围。在更现实的场景中,您可以通过Child1的 MdiParent 属性获取引用。



因此您的处理程序可能看起来像这个:



<前lang =cs> 私人 void btnOpen_Click( object sender,EventArgs e)
{
MainForm mainForm = 这个 .MdiParent as MainForm; // this是Child1,它引用它的MdiParent
MainForm.child2 = new Child2();
MainForm.child2.MdiParent = fMainForm;
MainForm.child2.Show();
}







还要考虑是否要创建一个新的Child2实例你点击按钮的时间,或者只是隐藏/重新显示同一个实例...



快乐编码



Johannes


I am a beginner on C# .NET, trying to code out something. I have three Windows.Form:

1.	MainForm (MDI_FORM)
2.	Child1
3.	Child2

On MainForm, I commanded Child1.Show()
Then from Child1, I intend to assign and open Child2 as a child to MainForm via the codes below:

    On MainForm, I declared:

         public static Child2 child2;


    Then on btnOpen from Child1, I coded:

         private void btnOpen_Click(object sender, EventArgs e)
            {
                MainForm mainForm = new MainForm();
                mainForm.child2 = new Child2 ();
                mainForm.child2.MdiParent = fMainForm;
                mainForm.child2.Show();
            }

Someone help me on what to do please.
It's not working.
Thanks a lot!!!

解决方案

Hi Salisu Shaibu,

Your code doesn't work because you are creating a new instance of your MainForm. The trick is to use a reference to the MainForm and set it as MdiParent to your second Form. Have a look at this (runnable) example:

using System;
using System.Windows.Forms;

namespace MdiOhMy
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Form formMain = new Form { IsMdiContainer = true };

            Form formChild1 = new Form { Text = "Child1", MdiParent = formMain, Visible = true };
            Button buttonShowChild2 = new Button { Text = "Open Child2", Parent = formChild1 };
            buttonShowChild2.Click += (object sender, EventArgs e) =>
                {
                    Form formChild2 = new Form { Text = "Child2", MdiParent = formMain, Visible = true };
                };

            Application.Run(formMain);
        }
    }
}



In this example I can reference the formMain instance because it's in the captured scope. In a more realistic Scenario you can obtain a reference via Child1's MdiParent property.

So your handler could look like this:

private void btnOpen_Click(object sender, EventArgs e)
 {
     MainForm mainForm = this.MdiParent as MainForm; // "this" is Child1 which refernces it's MdiParent
     MainForm.child2 = new Child2();
     MainForm.child2.MdiParent = fMainForm;
     MainForm.child2.Show();
 }




Also think about if you want to create a new instance of Child2 every time you click your button, or just hide/reshow the same instance...

Happy coding

Johannes


这篇关于如何从FormB打开FormC作为FormA的MDI_Child的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆