using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace SelfHelpClient { public partial class DialogForm : Form { System.Timers.Timer timer; int sec = 0; private DialogForm(string msg, int closeSec) { InitializeComponent(); msgLbl.Text = msg; okBtn.Text = "确定"; if (closeSec > 0) { sec = closeSec; timer = new System.Timers.Timer(); timer.Interval = 1000; timer.Elapsed += timer_Elapsed; timer.Start(); } this.FormClosing += DialogForm_FormClosing; } void DialogForm_FormClosing(object sender, FormClosingEventArgs e) { if (timer != null) { if (timer.Enabled) timer.Enabled = false; timer.Dispose(); } } bool seted = false; void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { this.Invoke(new Action(() => { if (sec > 0) { if (!seted) { this.okBtn.Location = new Point(20, 23); seted = true; } this.okBtn.Text = string.Format("确定({0}秒)", sec); sec--; } else { timer.Enabled = false; this.Close(); } })); } public static void ShowDialog(string msg, int closeSec = 0) { var d = new DialogForm(msg, closeSec); d.ShowDialog(); } private void okBtn_Click(object sender, EventArgs e) { this.Close(); } } }