Rabeea qabaha
Rabeea qabaha

Reputation: 606

Why FormClosing causing loop

I have Restaurant POS, on the sales screen I create a new invoice on form load, and in closing, I delete the current invoice if there are no items inserted into the invoice but if there are items I ask the user if he is really sure he wants to close the invoice and cancel the invoice if yes, I delete the invoice if no I keep him in the sales screen. on form closed I checked the user type if it an employee I close the entire app and if it an admin I close the form normally so he can get back to home form

but in form closing, the code executed 2 times and I couldn't figure why, knowing that the code was looping infinity but I improved the code but it's still executed 2 times now.

here is my code:

 private void Sales_screen_FormClosed(object sender, FormClosedEventArgs e)
    {
        DBConn.Delete("Delete from invoices where ID=@ID", CommandType.Text, new SqlParameter[] { new SqlParameter("@ID", INVOICEID.ToString()) });
        if (Curent_user.Utype == "employee")
        {
            try
            {
                Environment.Exit(0);
            }
            catch (Exception)
            {
            }
        }
        else
        {
            Close();
            Dispose();
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
    }

    private void Sales_screen_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (GV.Rows.Count > 0)
        {
            var result = CmessageBox.ShowDialog(this, "confirmation", "are you sure you want to close the invoice؟");
            if (result != DialogResult.Yes)
            {
                e.Cancel = true;
            }
        }
    }

Upvotes: 0

Views: 67

Answers (1)

Marc Roussel
Marc Roussel

Reputation: 499

By removing the Close() line in the else it shouldn't loop

Upvotes: 1

Related Questions