簡體   English   中英

如何在父表單上停止計時器(C#)

[英]How to stop Timer on parent form (C#)

我在第一個窗體上有Timer控件(和一個Button)。

想法是,當用戶點擊按鈕時,會顯示帶有新按鈕的新模態窗口,如果他想要在點擊模態表單按鈕時停止計時器,則會有疑問。

你能幫助我如何實現這一目標嗎?

您還可以根據答案在表單上設置帶有問題的DialogResult,並在主對話框中檢查結果。

ConfirmationForm confirmationForm = new ConfirmationForm();

DialogResult dialogResult = confirmationForm.ShowDialog();
if (dialogResult == DialogResult.OK)
{
  // Stop timer here
}

或使用標准對話框

DialogResult dialogResult = MessageBox.Show("Are you sure you want to stop the timer?", "Stop timer", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
  // Stop timer here
}

編輯:將confirmationForm.Show更改為confirmationForm.ShowDialog。

您可以在第二個表單上添加一個屬性來接收第一個表單的計時器:

    private Timer timer;
    public frmModal(Timer tmrref)
    {
        this.timer = tmrref;
        InitializeComponent();
    }

然后,當您顯示模態表單時,您可以傳遞計時器:

frmModal frm = new frmModal( this.timer1);
frm.ShowDialog();

然后在按鈕上單擊模態表單只需停止計時器:

timer.Stop();

在名為bStopTimer形式中聲明一個Boolean屬性

如果用戶按“是”,則將bStopTimer設置為True否則設置為False

關閉子表單時,請檢查childForm.bStopTimer並根據它執行操作。

以模態形式聲明Public Boolean屬性。 從主窗體中使用循環UNTIL,布爾值為True

While not MyVal = true
   ModalForm.show
End While

使用此方法,您還可以從主窗體和模態窗體啟動或停止計時器。 你會得到點:)

問候!

暫無
暫無

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

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