我想从另一个子窗体事件中关闭子窗体

本文关键字:窗体 事件 另一个 | 更新日期: 2023-09-27 18:36:47

我有一个弹出显示数据的孩子。

但是,当数据更改时,将创建一个新窗体来显示新数据。

我想关闭旧表单,这样我就不会在每次数据更改时都得到 5000 个表单。

创建新表单的原因是可以在其名称中显示数据的 ID。

我的代码:

String Pass; // used to get value from class and pass it to next form.
public void ShowNewCompareDiff() //object sender, EventArgs e
{
    FormCompareDiff childForm = new FormCompareDiff(Pass);
    childForm.MdiParent = MdiParent;
    childForm.Text = "Comepare difference ";
    //childForm.Close(); //Not working 
    //childForm = null; //Not working
    childForm.Show();
}
private void dataGridViewResult_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
    CompareXML Com = new CompareXML();
    Pass = Com.Compare(richTextBoxSQL.Text, richTextBoxPrevSQL.Text);
    ShowNewCompareDiff();
}

子窗体 FormCompareDiff:

namespace AuditIT_1
{
    public partial class FormCompareDiff : Form
    {
        String Passed;
        public FormCompareDiff(String Pass)
        {
            Passed = Pass;
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Location = new System.Drawing.Point(836, 0); // Form Shows next to FormSchemaSearch
            InitializeComponent();
        }
        private void FormCompareDiff_Load(object sender, EventArgs e)
        {
            String Pass = Passed;
            CompareXML Com = new CompareXML();
            webBrowserDifferences.DocumentText = Com.ResultShow(Pass);
        }       
    }    
}

我想从另一个子窗体事件中关闭子窗体

您可以将childForm转换为类的成员变量,然后将ShowNewCompareDiff方法更改为如下所示的内容:

FormCompareDiff childForm;
public void ShowNewCompareDiff()
{
    if (childForm != null)
        childForm.Dispose(); // Get rid of old form, if exists
    childForm = new FormCompareDiff(Pass);
    childForm.MdiParent = MdiParent;
    ...
}

编辑:

完整示例

创建一个新WindowsFormApplication,向Form添加新Button,添加以下代码:

public partial class Form1 : Form
{
    Form MyForm;
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        if (MyForm != null)
            MyForm.Dispose();
        MyForm = new Form() { Text = DateTime.Now.ToString() };
        MyForm.Show();
    }
}        

测试它。你会看到它是如何工作的。

您需要

显式跟踪子窗体的生命周期。 并使用属性而不是构造函数参数,以便可以更新子项,而不必创建新的子项。 喜欢这个:

FormCompareDiff childForm;
public void ShowNewCompareDiff()
{
    if (childForm != null) {
        childForm.WindowState = FormWindowState.Normal;
    }
    else {
        childForm = new FormCompareDiff();
        childForm.MdiParent = MdiParent;
        childForm.FormClosed += delegate { childForm = null; };
    }
    childForm.Pass = Pass;
    childForm.Show();
}

不要忘记将公共 Pass 属性添加到 FormCompareDiff。

在您的 ShowNewCompareDiff() 方法上,您创建的 FormCompareDiff 表单对象将其保留为主表单上的属性,然后您可以在每次数据更改时更新相同表单的文本,如果该属性与 child from 为 null,则创建一个新属性并重新分配给它。

编辑

public void ShowNewCompareDiff(Form formToClose) 
{
    FormCompareDiff childForm = new FormCompareDiff(Pass);
    childForm.MdiParent = MdiParent;
    childForm.Text = "Comepare difference ";
    childForm.Show();
    formToClose.Close(); // close the form you want
}

将此方法称为

ShowNewCompareDiff(this);