0

I have a child that pops up to display data.

but when the data changes a new form is created to display the new data.

I want to close the old form, so i don't end up with 5000 forms every time the data changes.

The reason a new form is created is so that in its name the data's ID can be shown.

My Code:

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

Child form 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);
        }       
    }    
}
Pomster
  • 14,567
  • 55
  • 128
  • 204

3 Answers3

2

You could convert childForm to a member variable of your class, and then alter your ShowNewCompareDiff method to something like this:

FormCompareDiff childForm;

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

    childForm = new FormCompareDiff(Pass);
    childForm.MdiParent = MdiParent;
    ...
}

EDIT:

Complete Example

Create a new WindowsFormApplication, add a new Button to the Form, add the following code:

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

Test it. You'll see how it works.

sloth
  • 99,095
  • 21
  • 171
  • 219
  • I got this error: Cannot access a disposed object. Object name: 'FormCompareDiff'. – Pomster Jun 12 '12 at 06:51
  • Cant have the if statement before the Child form new,Cannot use local variable 'childForm' before it is declared – Pomster Jun 12 '12 at 06:52
  • WindowsForms and MdiChild forms behave differently. If you look at my above code, i cant insert you If, its not the same. – Pomster Jun 12 '12 at 07:31
  • @Pomster So the ShowNewCompareDiff method is inside the MdiChild form? – sloth Jun 12 '12 at 07:39
  • Its in a different child form to the one i want to close. – Pomster Jun 12 '12 at 07:41
  • @Pomster So you are creating a new child form within a child form, and close the one that created the new one, right? – sloth Jun 12 '12 at 07:42
  • Yes, creating a new child form from a child form, but both child forms will exist with in a MDI parent container. – Pomster Jun 12 '12 at 07:44
  • And you want to close the old one so that there's only one form inside the MDI parent? – sloth Jun 12 '12 at 07:45
  • No not all child forms must close, just the previous child form FormCompareDiff, and a new previous child form FormCompareDiff must then be created. – Pomster Jun 12 '12 at 07:48
  • Closed the wrong child form, closed the one i'm using, as i said i'm using one child form to close another. – Pomster Jun 12 '12 at 07:53
  • Sorry, but i will give up now. I rolled back my answer to the previous version, which clearly showed how to keep track of the child form and how to replace it. – sloth Jun 12 '12 at 07:57
1

You'll want to explicitly keep track of the life of the child form. And use a property instead of a constructor argument so you can update the child instead of having to create a new one. Like this:

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

Don't forget to add a public Pass property to FormCompareDiff.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

on your ShowNewCompareDiff() method ones you create FormCompareDiff form object keep it as property on the main form, then you can update Text of the same form every time data changes, if that property with child from is null create new one and assign back to it.

EDIT

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
}

call this method as

ShowNewCompareDiff(this);
Damith
  • 62,401
  • 13
  • 102
  • 153
  • The form name and caption are the ID of the data i'm viewing, so can't just change content, i need it as a new form. – Pomster Jun 12 '12 at 06:54
  • The ShowNewCompareDiff() is running on a different child form, so i cant this.close, would be great if i could that.close – Pomster Jun 12 '12 at 08:09