有人请帮我弄清楚如何从显示在mdi环境之外的表单加载mdi子表单。 [英] Someone please help me to figure out how i can load an mdi child form from a form which displays outside the mdi environment.

查看:54
本文介绍了有人请帮我弄清楚如何从显示在mdi环境之外的表单加载mdi子表单。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个基于mdi的应用程序,某些表单必须显示在mdi内部,而其他表单必须显示在mdi之外。我最大的问题是在mdi环境之外的一个表单中显示我的mdi中的表单(我之前已经显示过,而我没有关闭它)。

I am making an mdi based application, some forms have to display inside the mdi while others outside the mdi. My big problem is to display a form inside my mdi(which i had displayed earlier and i had not closed it) from a form which is outside the mdi environment and is active.

推荐答案

这取决于内部的含义。无论如何,MDI和非MDI不能很好地融合在一起。这是一个简单的想法:谁需要MDI?为什么要折磨自己并吓跑你的用户呢?



自己帮个忙:不要使用MDI。没有它,您可以更轻松地实现设计,质量更好。 MDI甚至被微软高度劝阻,事实上,微软将其从WPF中删除并且很难支持它。更重要的是,如果您使用MDI,您将吓跑所有用户。只是不要。请参阅:

http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages [ ^ ],

如何在WPF中创建MDI父窗口? [ ^ ]。



我可以解释做什么。请看我过去的答案:

如何在WPF中创建MDI父窗口? [解决方案2 ],

关于在WPF中使用MDI窗口的问题 [ ^ ],

麦当劳给出错误 [ ^ ],

如何设置子窗体最大化,最后一个子窗体最小化 [ ^ ]。



-SA
It depends what would you mean by "inside". Any way, MDI and non-MDI don't live well together. Here is the simple idea: who needs MDI, ever? Why torturing yourself and scaring off your users?

Do yourself a great favor: do not use MDI at all. You can do much easier to implement design without it, with much better quality. MDI is highly discouraged even by Microsoft, in fact, Microsoft dropped it out of WPF and will hardly support it. More importantly, you will scare off all your users if you use MDI. Just don't. Please see:
http://en.wikipedia.org/wiki/Multiple_document_interface#Disadvantages[^],
How to Create MDI Parent Window in WPF?[^].

I can explain what to do instead. Please see my past answers:
How to Create MDI Parent Window in WPF? [Solution 2],
Question on using MDI windows in WPF[^],
MDIContainer giving error[^],
How to set child forms maximized, last childform minimized[^].

—SA


我也认为使用MDI是可以避免的,但是,事实是很多人仍然使用它,仍然需要维护/改变使用它编写的代码,以及许多学生被分配了创建MDI WinForms应用程序的任务。



没有技术原因......正确的应用程序设计...任何表单...... MDIChild ,或独立...不能与任何其他表格互动。



要回答您的问题,我们需要知道您的设计是什么,MDIChild如何并创建外部表单。请显示一些代码。



我将向您展示一种方法,您可以尝试使外部窗体触发对MDIChildForm产生影响的操作。



主MDI表格:
I, also, think using MDI is something to be avoided if possible, but, the fact is many people still use it, still need to maintain/alter code written using it, and many students are assigned the task of creating an MDI WinForms app.

There is no technical reason why ... with proper application design ... that any Form ... MDIChild, or "independent" ... cannot "interact" with any other Form.

To answer your question, we need to know what your design is, how the MDIChild and "external" Forms are created. Please show some code.

I'll show you one way you might go about the task of making an External Form trigger an action that has an effect on an MDIChildForm.

Main MDI Form:
using System;
using System.Windows.Forms;

namespace TestMDIApp_Nov28_2013
{
    public partial class MDIParentForm : Form
    {
        public MDIParentForm()
        {
            InitializeComponent();
        }

        // an independent, "external" Form
        ExternalForm externalForm1 = new ExternalForm();

        // an MDI Child Form
        Form mdiChildForm1 = new Form();

        private void MDIParentForm_Load(object sender, EventArgs e)
        {
            mdiChildForm1.MdiParent = this;
            mdiChildForm1.Text = "First Born MDIChild";
            mdiChildForm1.Show();

            // insert a reference to the ShowTheChildinMDIForm
            // method into the external Form [0],[1]

            // direct assignment of method to Action in external Form
            externalForm1.ShowTheChildOnMainMDIForm = ShowHiddenMDIChild;

            // anonymous method assignment of method to Action in external Form
            // using delegate
            // externalForm1.ShowTheChildOnMainMDIForm = delegate() { mdiChildForm1.Show();};

            // lambda expression assignment of method to Action in external Form
            // externalForm1.ShowTheChildOnMainMDIForm = () => ShowHiddenMDIChild();

            externalForm1.TopMost = true;
            externalForm1.Show();
        }

        // for educational purposes only: putting Controls
        // directly on an MDIParent Form is a very bad idea !
        private void button1_Click(object sender, EventArgs e)
        {
            mdiChildForm1.Hide();
        }

        // this is the method that we insert into the external Form
        // in the MDIForm Load EventHandler above using the first
        // form of assignment to Action
        private void ShowHiddenMDIChild()
        {
            mdiChildForm1.Show();
        }
    }
}

外部表单示例:

using System;
using System.Windows.Forms;

namespace TestMDIApp_Nov28_2013
{
    public partial class ExternalForm : Form
    {
        public ExternalForm()
        {
            InitializeComponent();
        }

        // The no-return-Type, no parameters, Action delegate
        // Type requires .NET 3.5 [0]
        public Action ShowTheChildOnMainMDIForm;

        private void button1_Click(object sender, EventArgs e)
        {
            // execute the Action
            ShowTheChildOnMainMDIForm();
        }
    }
}

注意:



0.这里使用的'Action Type在ExternalForm中对于没有返回类型且没有参数的委托来说,它是简称。在.NET 2.0中引入通用动作类型之后,这种非泛型动作类型被添加到.NET中



1.三可以显示如何将操作分配给外部窗体中的类型操作的公共字段。第一个(活动)只是指定一个指向以通常方式定义的方法的指针。第二种技术(注释掉)使用委托来指定匿名方法,第三种技术(注释掉)使用Lambda表达式来指定匿名方法。



在正式术语中,我们在这里做的是将方法(或匿名方法body)的引用注入到Action类型的公共字段中。



您可能会问:为什么要解决所有这些问题,当您只需在ExternalForm中设置对MDIParent表单的引用...或者在MDIParent表单中创建一个任何其他表单可能的静态变量请参阅?



这个想法是,你只揭露一些实体需要知道的某些其他实体,这是关注点分离原则的一种形式,imho。

Notes:

0. The 'Action Type used here in ExternalForm is "short-hand" for a delegate with no return Type, and no parameters. This "non-generic" 'Action Type was added to .NET in FrameWork 3.5, after the introduction of the generic Action Types in .NET 2.0

1. the three ways you could assign an action to take to the Public field of Type 'Action in ExternalForm are shown. the first (active) just assigns a pointer to a method defined in the usual manner. the second technique (commented out) uses a delegate to assign an "anonymous" method, and the third technique (commented out) uses a Lambda Expression to assign an anonymous method.

In "formal" terms, what we're doing here is injecting a reference to a method (or to an anonymous method "body") into a Public Field of Type 'Action.

You might ask: "why go to all this trouble, when you could just set a reference to the MDIParent Form in the ExternalForm ... or create some static variable in the MDIParent Form that any other Form could "see" ?

The idea is that you expose only what some entity "needs to know" about some other entity which is a form of the "principle of separation of concerns," imho.


这篇关于有人请帮我弄清楚如何从显示在mdi环境之外的表单加载mdi子表单。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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