带图像的C#FORM和自动关闭

时间:2016-05-16 17:46:19

标签: c# c#-4.0

我遇到了一个问题,我创建了一个新的表单,带有后台img,以及我需要的所有内容以及我想要的工作,但是我还需要在5或10秒后自动关闭它。

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

你能帮助我吗? 我现在绝望了......自从我尝试以来差不多10个小时。 你是我最后的希望。 感谢

this.close()选择做了,或者我做错了,但我对此表示怀疑。 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();
        }
    }
}

2 个答案:

答案 0 :(得分: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请评论这个实现是否最终对你有用。

答案 1 :(得分:1)

我会在您的表单上放置一个PictureBox和计时器(设置为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();
        }
    }
}