2009-01-05 20 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));