You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

72 lines
1.5 KiB

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();
}
}
void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
this.Invoke(new Action(() =>
{
if (sec > 0)
{
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();
}
}
}