2009-01-05 21 views

回答

16

贝丽已张贴在我花了测试这个时间类似的代码,但这里是我的尝试:

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; 
+0

比我的清洁方法。 +1 – BFree 2009-01-05 01:07:04

3

在类级别做这样的事情:

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(); 
     } 
8

快速和肮脏的解决方案利用关闭。无需定时器!

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(); 
    } 

实施例:

//使形式5秒

Invisibilize不可见的(新的时间跨度(0,0,5));