Current location - Health Preservation Learning Network - Slimming men and women - C# MessageBox。 How does Show () set to close automatically after 3 seconds?
C# MessageBox。 How does Show () set to close automatically after 3 seconds?
Yes, here are some screenshots and source code. The complete source code is attached:

1. Specify the message to pop up and the timing time (in seconds).

2. After the pop-up, the OK button on the dialog box will dynamically count down, and it will automatically close when the time is 0, or you can click the OK button to close it.

Core code:

? Public? Part? Class? TimingMessageBox? :? form

{

//? Time limit for automatic shutdown, for example, 3 means automatic shutdown after 3 seconds.

Private? int? Second;

//? A counter used to determine the duration after the current window pops up.

Private? int? Counter;

//? constructors

Public? TimingMessageBox (string? News? int? Second)

{

initialize component();

//? Display message

this.labelMessage.Text? =? News;

//? Acquisition time limit

This, the second one? =? Second;

//? Initialization counter

This counter? =? 0;

//? The text of the initialization button

this.buttonOK.Text? =? String. Format ("OK ({0})",? This, the second one? -? this . counter);

//? Activate and start the timer, and set the trigger interval of the timer to 1000ms (1s).

this.timer 1。 Enable? =? True;

this.timer 1。 Interval? =? 1000;

this.timer 1。 start();

}

Private? Invalid? Timer 1_Tick (object? Sender? EventArgs? e)

{

//? If the specified time limit is not reached

What if? (this.counter? & lt=? This second)

{

//? Refresh the text of the button

this.buttonOK.Text? =? String. Format ("OK ({0})",? This, the second one? -? this . counter);

This. Refresh ();

//? Counter self-increment

this . counter++;

}

//? If the time limit is reached

other

{

//? Turn off the timer

this.timer 1。 Enable? =? Fake;

this.timer 1。 stop();

//? Close the dialog box

This. close();

}

}

Private? Invalid? ButtonOK_Click (object? Sender? EventArgs? e)

{

//? Click the OK button to close the dialog box.

This. close();

}

} and then call in the main form:

Public? Part? Class? Form 1? :? form

{

Public? Form 1 ()

{

initialize component();

}

Private? Invalid? ButtonShowMessageBox_Click (object? Sender? EventArgs? e)

{

String? Leave a message =? this . textbox message . text . trim();

int? Second? =? Conversion. toint 32(this . textbox second . text . trim());

TimingMessageBox? messageBox=new? TimingMessageBox (message, seconds);

messageBox。 ShowDialog();

}

}