Ryanagray
Ryanagray

Reputation: 95

Close a windows form then refresh another form

I am trying to write a program that adds stock to a database,

The program wokrs to add stock and show stock, the issue im having is that when i clsoe my add stock form i would like to refresh the main form to show the new current stock state.

    this.FormClosing += new FormClosingEventHandler(AddStock_FormClosing);

    private void AddStock_FormClosing(object sender, FormClosingEventArgs e)
    {
        Program.FORM1.ShowStock();
        MessageBox.Show("Tries to close");
        this.Close();
    }  

When i close the form i get the message box so i know its getting to that point, my problem is that the message box keeps reopening and does not actually close the form.

Upvotes: 0

Views: 2421

Answers (2)

Kai Hirst
Kai Hirst

Reputation: 11

Your using the Messagebox.Show in the wrong context. Drop the Messagebox AFTER the this.close() and you'll find that it should be all good..

private void AddStock_FormClosing(object sender, FormClosingEventArgs e)
    {
        Program.FORM1.ShowStock();
        this.Close();
        MessageBox.Show("Tries to close");

    }  

What I would do is change the message box for the confirmation message asking to close or not. Then Id use an IF statement to ascertain whether the return choice is true or not, then close the form and perform garbage collection (If necessary) if the confirmation returns false, return to the form without doing anything..

Upvotes: 0

Edgar Hernandez
Edgar Hernandez

Reputation: 4030

The FormClosing event is fired every time the Close method is called. If you don't cancel the closing event, the form will be closed and that's it. You don't need to call again this.Close().

This is enough.

this.FormClosing += new FormClosingEventHandler(AddStock_FormClosing);

private void AddStock_FormClosing(object sender, FormClosingEventArgs e)
{
    Program.FORM1.ShowStock();
} 

Upvotes: 1

Related Questions