0

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

但是当数据发生变化时,会创建一个新表单来显示新数据。

我想关闭旧表单,所以每次数据更改时我都不会得到 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);
        }       
    }    
}
4

3 回答 3

2

您可以转换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,添加一个新ButtonForm,添加以下代码:

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();
    }
}        

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

于 2012-06-12T06:48:10.837 回答
1

您需要明确跟踪子表单的生命周期。并使用属性而不是构造函数参数,这样您就可以更新孩子而不必创建新的。像这样:

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。

于 2012-06-12T07:10:55.977 回答
0

在您的 ShowNewCompareDiff() 方法上,您创建 FormCompareDiff 表单对象将其作为主表单上的属性,然后您可以在每次数据更改时更新同一表单的文本,如果该属性的子属性为空,则创建新的并分配回它。

编辑

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);
于 2012-06-12T06:52:59.490 回答