我正在尝试在c#中让一个表单在x时间内不可见。 有什么想法吗?
谢谢, Jon
this.Hide();
var t = new System.Windows.Forms.Timer
{
Interval = 3000 // however long you want to hide for
};
t.Tick += (x, y) => { t.Enabled = false; this.Show(); };
t.Enabled = true;
利用闭包实现快速而简单的解决方案。无需使用 Timer
!
private void Invisibilize(TimeSpan Duration)
{
(new System.Threading.Thread(() => {
this.Invoke(new MethodInvoker(this.Hide));
System.Threading.Thread.Sleep(Duration);
this.Invoke(new MethodInvoker(this.Show));
})).Start();
}
示例:
// Makes form invisible for 5 seconds.
Invisibilize(new TimeSpan(0, 0, 5));
Timer timer = new Timer();
private int counter = 0;
public Form1()
{
InitializeComponent();
timer.Interval = 1000;
timer.Tick += new EventHandler(timer_Tick);
}
void timer_Tick(object sender, EventArgs e)
{
counter++;
if (counter == 5) //or whatever amount of time you want it to be invisible
{
this.Visible = true;
timer.Stop();
counter = 0;
}
}
然后,在您想将其隐藏的任何位置(我将在此处演示按钮单击):
private void button2_Click(object sender, EventArgs e)
{
this.Visible = false;
timer.Start();
}