自定义延时关闭弹窗,替代MesssageBox
1,新建一个窗体MessageForm,在里面加一个label控件和timer
2,代码如下:
public partial class MessageForm : Form { int t; string txt; /// <summary> /// 自定义弹窗 /// </summary> /// <param name="time">窗体消失时间</param> /// <param name="text">窗体提示内容</param> public MessageForm(int time, string text) { t = time; txt = text; InitializeComponent(); } private void timer1_Tick(object sender, EventArgs e) { this.Close(); } private void MessageForm_Load(object sender, EventArgs e) { label1.Text = txt; timer1.Interval = t; timer1.Start(); } }
3,在其他窗体调用:
MessageForm mf = new MessageForm(3000, "输入参数有误"); mf.Show();