简体   繁体   中英

how to close the mainform with all open dialogs in winform

I am creating an application to be used in restaurants. A waiter comes to the terminal and opens a session, then he enters an order for a customer. the waiter might be in rush and might leave the terminal with an open session. I am getting to IDLE Time of the computer and close the session if it exceeds 10 seconds.

My problem is that the waiter can open dialog popups, I can close the main form with "this.Close()" but I cannot close the dialog popups.

How can I close the dialog popups as well?

here is my code that listens to the IDLE time

uint idleTime = IdleTimeFinder.GetIdleTime();
TimeSpan timespent = TimeSpan.FromTicks(idleTime);
int timespent_in_sec = Convert.ToInt32(timespent.TotalMilliseconds * 10000);
if (timespent_in_sec > IdleTime)
{
    //close dialogs if there is one

    this.Close();
}

You should keep a list of every Form you open, so you can close it when you close the master form.

Just hook on the OnClosed event and walk over the list:

List<Form> formsToClose = new List<Form>();

public Form1()
{
    InitializeComponent();

    Form f = new Form();
    f.Show();

    formsToClose.Add(f);
}

protected override void OnClosed(EventArgs e)
{
    base.OnClosed(e);

    foreach (Form f in formsToClose)
    {
        f.Close();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM