简体   繁体   中英

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.

what i want is to add or start a count down when a messagebox pop up, the default yes button was disabled. and after 5 second the yes button , become enable and ready to click by user.

留言框

  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. Open the porperties tab. Set properties as give below in point 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). Write code to start timer on Shown event of form:

     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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM