如何关闭activeMidForm并打开另一个?

时间:2015-01-24 05:26:09

标签: c# winforms forms

我有一个方法,我使用打开一个Mid Child。

让我们说我在一个名为" InventoryAdd"在这个表格上我有一个下拉菜单。当用户选择一个值" -1"从菜单中,我想打开另一个(即DepartmentsAdd()。)然后在DepartmentAdd打开之后我想关闭InventoryAdd表单。

这是InventoryAdd表单上菜单背后的代码

private void InputDepartment_SelectedValueChanged(object sender, EventArgs e) {
    //InputDepartment.ValueMember = "ID";
    int selectedDept = Convert.ToInt32(InputDepartment.SelectedValue);

    if (selectedDept == -1) {
        Form myForm = this.ActiveMdiChild;

        Common.OpenMyForm("Vendors", new string[] { "add" }, new DepartmentsAdd());

        if (myForm != null) {
            myForm.Close();
        }
    }
}

此方法打开新的Mid表单

    public static void OpenMyForm(string sectionName, string[] keys, Form myform) {
        //make sure there are no other forms of the ame type open
        foreach (Form form in Application.OpenForms) {
            if (form.GetType() == myform.GetType()) {
                form.Activate();
                return;
            }
        }

        if (Settings._AuthenticationMode == "Thumbprint") {

            var newMDIChild = myform;

            // Set the Parent Form of the Child window.
            newMDIChild.MdiParent = AppContext.CurrentContext.MainForm;

            // Display the new form.
            newMDIChild.Show();
        }


        if (Settings._AuthenticationMode == "Single" && UserInfo.Autherized == true) {

            var role = new Roles();

            if (role.hasAccess(sectionName, keys)) {
                var newMDIChild = myform;

                // Set the Parent Form of the Child window.
                newMDIChild.MdiParent = AppContext.CurrentContext.MainForm;

                // Display the new form.
                newMDIChild.Show();
            }
            else {
                Common.Alert("You do not have a permissions to perform this action!");
            }
        }
    }
}

我的代码打开Mid表单,没有任何问题。然而," InventoryAdd"形式永远不会关闭" myForm的"永远不会关闭它。

如何正确关闭Mid表格?

由于

2 个答案:

答案 0 :(得分:0)

如果在调用上下文中新打开的表单上为shown设置了事件侦听器,则可以在打开子节点后使现有表单自行关闭。

Form myForm=new Form();
myForm.shown += closeForm;
...

void closeForm(object sender, EventArgs e){
this.Dispose(); // as long as within the previous           forms context.
}

答案 1 :(得分:0)

myForm.Close();

正在关闭程序尝试使用

myForm.Hide();