且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在子窗体关闭时以mdi父窗体显示面板。

更新时间:2023-10-09 11:39:28

以下是MDI WinForms风味应用程序中的策略大纲... 。使用三种表格。结构:



a。一个名为'TheMdiParentForm的表单,其'MDIParent属性设置为'true



a.1两个表单,'DefaultChildForm和'ChildForm1



a.1.1'DefaultChildForm是你想要出现的表格,占据TheMDIParentForm的所有客户区



a.1.2'ChildForm1 is您希望按需出现的其他表格。



a.1.3。这两个表格的实例在表格范围内宣布



设计时间:



两个子表格他们的'ControlBox属性设置为false,因此用户无法以通常的方式在运行时关闭它们。



初始状态:



b。当TheMdiParentForm加载时:



b.1。创建'DefaultChildForm和'ChildForm1的实例并将其分配给在a.1.3中创建的实例:他们的'MdiParent属性设置为'TheMdiParentForm



b.1.1。 'DefaultChildForm的实例在TheMdiParentForm的客户区显示为全屏



b.1.1.1一个按钮,此表单上的'ChildForm1OpenButton':将使' ChildForm1可见,然后隐藏'DefaultChildForm



b.1.2。 'ChildForm1的实例将其visible属性设置为'false:未显示。



b.1.2.1此窗体上的按钮'ChildForm1CloseButton:关闭窗体



运行时行为



c.1用户点击'DefaultChildForm'上的'打开按钮:它被隐藏,并显示'ChildForm1。



c.2用户点击ChildForm1上的'关闭按钮:它被隐藏,并显示DefaultChildForm。



c.3用户点击TheMdiParentForm上的标准Windows Close CheckBox:应用程序终止。



注意:



a。无论你想在'Panel中使用什么,你都会提到'DefaultChildForm。



b。您必须处理将您需要的任何数据从两个子表单中的任何一个传递回主MdiForm。我强烈建议你不要直接在'DefaultChildForm和'ChildForm1之间传递任何数据。



那么,我们如何在Child Forms和之间实现这种沟通 MdiParent?



a。在'ChildForm1的代码中:
Here's an outline of a strategy ... within an MDI WinForms flavor app ... for using three Forms. The structure:

a. a Form named 'TheMdiParentForm which has its 'MDIParent Property set to 'true

a.1 two Forms, 'DefaultChildForm, and 'ChildForm1

a.1.1 'DefaultChildForm is the Form you want to appear occupying all the client area of TheMDIParentForm

a.1.2 'ChildForm1 is the "other" Form that you want to appear "on demand."

a.1.3. instances of both of these Forms are declared at Form scope

Design-time:

Both child Forms have their 'ControlBox properties set to false, so the user cannot close them at run-time in the usual way.

Initial state:

b. when TheMdiParentForm loads:

b.1. instances of 'DefaultChildForm and 'ChildForm1 are created and assigned to the instances created in a.1.3: their 'MdiParent property is set to 'TheMdiParentForm

b.1.1. the instance of 'DefaultChildForm is shown full-screen in the client area of TheMdiParentForm

b.1.1.1 a button, 'ChildForm1OpenButton' on this Form: will make 'ChildForm1 visible, and then hide the 'DefaultChildForm

b.1.2. the instance of 'ChildForm1 has its visible property set to 'false: is not shown.

b.1.2.1 a button 'ChildForm1CloseButton on this Form: closes the Form

Run-time behavior

c.1 the user clicks the 'Open button on the 'DefaultChildForm: it is hidden, and 'ChildForm1 is displayed.

c.2 the user clicks the 'Close button on ChildForm1: it is hidden, and the DefaultChildForm is displayed.

c.3 the user clicks the standard Windows Close CheckBox on the TheMdiParentForm: the application terminates.

Notes:

a. Whatever you wanted to use in the 'Panel you mention is put on the 'DefaultChildForm.

b. you will have to deal with passing back any data you require from either of the two Child Forms to the main MdiForm. I strongly recommend you do not implement passing any data between the 'DefaultChildForm and the 'ChildForm1 directly.

So, how do we implement this "communication" between Child Forms and MdiParent ?

a. in 'ChildForm1's code:
public Action CloseButtonClick { set; get; }

private void ChildForm1CloseButton_Click(object sender, EventArgs e)
{
    if (CloseButtonClick != null) CloseButtonClick();
}

b。在'DefaultChildForm的代码中:

b. in 'DefaultChildForm's code:

public Action OpenButtonClick { set; get; }

private void ChildForm1OpenButton_Click(object sender, EventArgs e)
{
    if (OpenButtonClick != null) OpenButtonClick();
}

在MDIParent的代码中:

In the MDIParent's code:

private DefaultChildForm DefaultChildFormInstance = new DefaultChildForm();
private ChildForm1 Child1FormInstance = new ChildForm1();

private void TheMdiParentForm_Load(object sender, EventArgs e)
{
    DefaultChildFormInstance.MdiParent = this;
    DefaultChildFormInstance.OpenButtonClick += OpenButtonClick;
    DefaultChildFormInstance.Show();
 
    Child1FormInstance.Visible = false;
    Child1FormInstance.MdiParent = this;
    Child1FormInstance.CloseButtonClick += CloseButtonClick;
}

private void CloseButtonClick()
{
    Child1FormInstance.Hide();
    DefaultChildFormInstance.Show();
}

private void OpenButtonClick()
{
    DefaultChildFormInstance.Hide();
    Child1FormInstance.Show();   
}

这是如何工作的?



a。我们将两个简单的Delegates(Type Action)注入到两个子窗体的实例中。当在任何一个子窗体中单击Button时,我们注入的Delegate实例被执行,并且调用MdiParentForm中定义的事件处理程序。



我们怎么样?将数据从子表单中的任何一个返回到MdiParentForm:我们可以使用类似的方法在子表单中定义公共委托......或者在子表单中定义公共结构,甚至是类。



我们如何发信号通知我们有一些有效数据要返回的MdiParentForm:我们可以轻松地更改我们的Action Delegate以传回一个布尔参数:在ChildForm1中:

How does this work ?

a. we inject two simple Delegates (Type Action) into the instances of the two Child Forms. When the Button is clicked in either of those Child Forms, the Delegate instance we injected is executed, and our Event Handlers defined in the MdiParentForm are called.

How would we return data from either of the Child Forms to the MdiParentForm: we could use a similar technique of defining a Public Delegate in the Child Forms ... or defining Public structures, or even a Class, in the Child Forms.

How would we "signal" the MdiParentForm that we had some valid data to be returned: we could easily change our Action Delegate to pass back a boolean parameter: in ChildForm1:

private HasData = false;

public Action<bool> CloseButtonClick { set; get; }

private void ChildForm1CloseButton_Click(object sender, EventArgs e)
{
    if (CloseButtonClick != null) CloseButtonClick(HasData);
}</bool>

并且,修改MdiParentForm以识别数据何时可用:

And, modify the MdiParentForm to recognize when data is available:

private void CloseButtonClick(bool hasData)
{
    if (hasData)
    {
        data.Clear();
        // explained below
        data = Child1FormInstance.GetData();
    }

    Child1FormInstance.Hide();
    DefaultChildFormInstance.Show();
}

在上面的代码中,在ChildForm1的实例中调用了一个Delegate Type Func:它看起来像这样:

In the above code, a Delegate Type Func is called in the instance of the ChildForm1: it looks like this:

public Func<List<string>> GetData { set; get; }

// see note below
private void ChildForm1_Load(object sender, EventArgs e)
{
    GetData = () => { return data; };
}

这个Func Delegate ...没有参数并返回一个Type of Type String ...在ChildForm1 Load EventHandler中设置,以返回当前的'data;在这段代码中,它使用'lambda notation'简写设置...创建一个匿名方法并绑定到'Func。

This Func Delegate ... that takes no parameters and returns a List of Type String ... is set in the ChildForm1 Load EventHandler to return the current 'data; in this code it is set using the 'lambda notation "shorthand" ... an anonymous method is created and bound to the 'Func.