2016-08-15 82 views
0

我在做一個記事本,我需要按兩張不同的圖片,並且需要保持3-5秒的可見狀態。我試過Thread.Sleep(5000),但它沒有顯示我第二個。我該怎麼辦? 我創建幾秒鐘就能看到圖像的唯一方法就是放置一個MessageBox但這不是我的想法,我不知道其他方式來做到這一點。如何讓兩張圖片在一定時間後不可見

if (pic != null && pic.Name == fondos[i].Name) 
{ 
    if (CantClick == 0) 
    { 
     ParejaActual = listRandom[i].pareja; 
     CantClick = 1; 
     primerI = i; 
     picAnterior = pic; 
     imgAnterior = img; 
     pic.Visible = false; 

    } 
    else if (CantClick == 1) 
    { 
     pic.Visible = false; 
     if (ParejaActual == listRandom[i].pareja) 
     { 
      SoundPlayer simpleSound = (new SoundPlayer(Configuracion.RootFolder + "aplau.wav")); 
      simpleSound.Play(); 
      Ganando++; 
      label3.Text = Ganando.ToString(); 
      //MessageBox.Show("Si"); 
      //NO SE DESTAPA LA SEGUNDA. 
      //Thread.Sleep(5000); 
      CantClick = 0; 
      img.Visible = false; 
      imgAnterior.Visible = false; 
      Application.DoEvents(); 

     } 
     else 
     { 

      (new SoundPlayer(Configuracion.RootFolder + "sad.wav")).Play(); 
      MessageBox.Show("No"); 
      Mal++; 
      CantClick = 0; 
      label4.Text = Mal.ToString(); 
      pic.Visible = true; 
      picAnterior.Visible = true; 
     } 
    } 
} 

謝謝!

+4

您需要在表單上使用System.Windows.Forms.Timer對象。不要在UI代碼中使用Thread.Sleep。 – PMF

回答

1

而不是使用Thread.Sleep,使用System.Timers類。間隔後,只需隱藏一個圖像並顯示其他圖像。告訴我你是否需要其他幫助。

+0

我無法再顯示timer_tick中的「pic」,因爲它是非公共變量。我能怎麼做? private void timer3_Tick(object sender,EventArgs e) { counter3--;如果(counter3 == 0) { CantClick = 0; picAnterior.Visible = true; picAnterior.Visible = true; Application.DoEvents(); counter3 = 3; timer3.Stop(); } } – sol

相關問題