簡體   English   中英

使用計時器在消息框中啟用按鈕

[英]Enabling Button in Messagebox with timer

我有一個消息框,當用戶單擊按鈕時彈出。 當用戶單擊“是”時,它將運行insert功能。

我想要的是在彈出messagebox時添加或開始倒數,默認的“ yes button被禁用。 5 second ,“ yes button變為enable並可供用戶單擊。

留言框

  if (MessageBox.Show("log", "test", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {                  
                insert();
            }

如評論中建議的那樣,您需要針對此功能擁有自己的實現。 下面是部分代碼,您將需要修改常規表單以使其看起來像對話框一樣:

  1. 將新Form添加到您的項目。 打開“屬性”選項卡。 設置屬性,如以下第2點所述。

  2. 在設計器中修改表單以將以下屬性更改為給定值:

     this.AcceptButton = this.btnYes;//To simulate clicking *ENTER* (Yes) this.CancelButton = this.button2; //to close form on *ESCAPE* button this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.MaximizeBox = false; this.MinimizeBox = false; //FROM CODEPROJECT ARTICLE LINK this.ShowInTaskBar = false; this.StartPosition = CenterScreen; 
  3. 添加一個計時器到窗體。 將其間隔設置為5000(5秒)。 編寫代碼以在表單的Shown事件上啟動計時器:

     private void DialogBox_Shown(object sender, EventArgs e) { timer1.Start(); } 
  4. 處理計時器的滴答聲:

     public DialogBox() { InitializeComponent(); //bind Handler to tick event. You can double click in //properrties>events tab in designer timer1.Tick += Timer1_Tick; } private void Timer1_Tick(object sender, EventArgs e) { btnYes.Enabled = true; timer1.Stop(); } 
  5. 設置按鈕處理程序:

     private void btnYes_Click(object sender, EventArgs e) { DialogResult = DialogResult.Yes; } 
  6. 在顯示此自定義消息框的位置,可以檢查是否單擊了“ Yes或“ No ,如下所示:

     var d=new DialogBox(); var result=d.ShowDialog(); if(result==DialogResult.Yes) //here you go.... 

暫無
暫無

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

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