繁体   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