最近在写一个小功能时脑袋一直转不过弯,就是想实现弹出一个窗体,然后提示“本窗体将在n秒后关闭!”这个小功能,找了好些博客,因为本人刚入门,大多都看不懂;煎熬了好几天后终于以一个简单的办法实现了;以下是关键代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace JUSHA.WEBQA.Client
{
public partial class FormNotice : Form
{
private int count = 10;
public bool CheckRightNow { get; set; }
public FormNotice()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CheckRightNow = true;
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
CheckRightNow = false;
this.Close();
}
private void FormNotice_Load(object sender, EventArgs e)
{
timer1.Interval = 1000;
this.timer1.Enabled = true;
this.timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
count--;
if (count == 0)
{
CheckRightNow = true;
this.Close();
}
else
{
label1.Text = $"Remote operation will be performed in {count} seconds";
}
}
}
}