[英]Enabling Button in Messagebox with timer
如評論中建議的那樣,您需要針對此功能擁有自己的實現。 下面是部分代碼,您將需要修改常規表單以使其看起來像對話框一樣:
將新Form
添加到您的項目。 打開“屬性”選項卡。 設置屬性,如以下第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;
添加一個計時器到窗體。 將其間隔設置為5000(5秒)。 編寫代碼以在表單的Shown
事件上啟動計時器:
private void DialogBox_Shown(object sender, EventArgs e) { timer1.Start(); }
處理計時器的滴答聲:
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(); }
設置是按鈕處理程序:
private void btnYes_Click(object sender, EventArgs e) { DialogResult = DialogResult.Yes; }
在顯示此自定義消息框的位置,可以檢查是否單擊了“ 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.