2016-05-16 41 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(); 
     } 
    } 
}