簡體   English   中英

如何用winform中所有打開的對話框關閉主窗體

[英]how to close the mainform with all open dialogs in winform

我正在創建要在餐廳中使用的應用程序。 服務員來到終端並打開會話,然后為客戶輸入訂單。 服務員可能會很忙並且可能會離開終端機進行開放會話。 我進入計算機的“空閑時間”,如果超過10秒,則關閉會話。

我的問題是服務員可以打開對話框彈出窗口,我可以使用“ this.Close()”關閉主窗體,但是無法關閉對話框彈出窗口。

如何關閉對話框彈出窗口?

這是我的代碼,監聽空閑時間

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

您應該保留每個打開的Form的列表,以便在關閉主表單時可以將其關閉。

只需掛接OnClosed事件並遍歷列表即可:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM