[英]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.