简体   繁体   English

使用计时器在消息框中启用按钮

[英]Enabling Button in Messagebox with timer

I have a message box that pops up when a user click a button. 我有一个消息框,当用户单击按钮时弹出。 when user click yes it's run an insert function. 当用户单击“是”时,它将运行insert功能。

what i want is to add or start a count down when a messagebox pop up, the default yes button was disabled. 我想要的是在弹出messagebox时添加或开始倒数,默认的“ yes button被禁用。 and after 5 second the yes button , become enable and ready to click by user. 5 second ,“ yes button变为enable并可供用户单击。

留言框

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

As suggested in the comment, you need to have your own implementation for this functionality. 如评论中建议的那样,您需要针对此功能拥有自己的实现。 Below is partial code that you will need to modify normal form to make it appear like dialogue box: 下面是部分代码,您将需要修改常规表单以使其看起来像对话框一样:

  1. Add new Form to your project. 将新Form添加到您的项目。 Open the porperties tab. 打开“属性”选项卡。 Set properties as give below in point 2 . 设置属性,如以下第2点所述。

  2. Modify form in designer to change following properties to given values: 在设计器中修改表单以将以下属性更改为给定值:

     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. Add a timer to form. 添加一个计时器到窗体。 Set its interval to 5000 (5 seconds). 将其间隔设置为5000(5秒)。 Write code to start timer on Shown event of form: 编写代码以在表单的Shown事件上启动计时器:

     private void DialogBox_Shown(object sender, EventArgs e) { timer1.Start(); } 
  4. Handle ticking of Timer: 处理计时器的滴答声:

     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. Set Yes button handler: 设置按钮处理程序:

     private void btnYes_Click(object sender, EventArgs e) { DialogResult = DialogResult.Yes; } 
  6. From where you are showing this custom message box, you can check if Yes or No is clicked as follows: 在显示此自定义消息框的位置,可以检查是否单击了“ 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