2016-05-16 43 views
1

我有一個問題,我做了一個新的形式,與背景img,和我需要和它的工作就像我想要的,但我也需要在5或10秒後自動關閉它。C#FORM與圖像和自動關閉

我在谷歌整天搜索......但沒有教程是好的。 我使用的是Visual Studio 2013.

你可以男孩幫我嗎請... 我現在絕望了......自從我嘗試了將近10個小時。 你是我最後的希望。 謝謝

this.close()dosen't做到了,或者我做錯了,但我懷疑這一點。 Application.Exit失敗 計時器給出錯誤......

//form 
using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Cerum_HS 
{ 
    public partial class CERUM_HS : Form 
    { 
     public CERUM_HS() 
     { 
      InitializeComponent(); 
      Rectangle r = Screen.PrimaryScreen.WorkingArea; 
      this.StartPosition = FormStartPosition.Manual; 
      this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height - this.Height); 
     } 
    } 
} 


//main. 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.Timers; 
//using System.Windows.Forms; 

namespace Cerum_HS 
{ 
    static class Program 
    { 
     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 


     private static System.Timers.Timer aTimer; 

     [STAThread] 
     static void Main() 
     { 
      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new CERUM_HS()); 

      aTimer = new System.Timers.Timer(); 
      aTimer.Interval = 10; 

      aTimer = new System.Timers.Timer(10); 

      aTimer.Elapsed += OnTimedEvent; 

      aTimer.AutoReset = false; 

      aTimer.Enabled = true; 
     } 

     private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e) 
     { 
      //Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime); 
      Application.Exit(); 
      //this.close(); 
     } 
    } 
} 
+0

發生了什麼?你在哪裏叫它?你得到了什麼錯誤? – SLaks

+0

我確實從這兩個文件調用了函數。有時候,它會導致錯誤,但應用程序關閉。我現在將發佈代碼。 –

+0

你在哪裏調用'this.Close()'?你不使用計時器嗎?可能運行在一個單獨的線程? –

回答

1

由於我的評論似乎幫助,我以爲我把它寫下來作爲一個答案。

public partial class CERUM_HS : 
{ 
    // here is the timer for the automatic closing 
    private static System.Timers.Timer aTimer; 

    public CERUM_HS() 
    { 
     InitializeComponent(); 
     Rectangle r = Screen.PrimaryScreen.WorkingArea; 
     this.StartPosition = FormStartPosition.Manual; 
     this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width, Screen.PrimaryScreen.WorkingArea.Height - this.Height); 
    } 

    private void Form_Load(object sender, System.EventArgs e) 
    { 
     // start here the timer when the form is loaded 
     aTimer = new System.Timers.Timer(); 
     aTimer.Interval = 10; 

     aTimer = new System.Timers.Timer(10); 

     aTimer.Elapsed += OnTimedEvent; 

     aTimer.AutoReset = false; 

     aTimer.Enabled = true; 
    } 

    private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e) 
    { 
     // Close the Application when this event is fired 
     Application.Exit(); 
    } 

} 

Bogdan請評論,如果這個實現是如何爲你工作的最後。

+0

well this.Close();將錯誤替換爲Application.Exit(); –

+0

好吧,變了!不錯的團隊精神。 –

1

我會把你的窗體上的圖片框和定時器(設置爲5000毫秒),點擊Tick事件,並使用此代碼:

namespace Image 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
      // set picture box to image of interest 
      // size and position form appropriately 
     } 

     private void timer1_Tick(object sender, EventArgs e) 
     { 
      timer1.Enabled = false; 
      this.Close(); 
     } 
    } 
}