Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I used a windows form c# and I want to put a timer to close dialog after 30 seconds..
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();


I have in main:
<pre>
namespace WindowsFormsApplicationDymoNew
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

           // string t = "";

            Form1 y = new Form1();
            Application.Run(y);

           

        }

      
    }
}




What I have tried:

Now I don't know how I can add time to this code
Posted
Updated 31-Mar-23 5:03am

1 solution

You have to set up a timer like this.
Googling produced many results "close windows form using timer"

C#
using Timer = System.Windows.Forms.Timer;

 private void Form1_Load(object sender, EventArgs e)
        {        
            // Timer to Close App
            Timer MyTimer = new Timer();   
            MyTimer.Interval = (1 * 60 * 1000); // 1 mins
            MyTimer.Tick += new EventHandler(timer1_Tick);
            MyTimer.Start();
        }


        private void timer1_Tick(object sender, EventArgs e)
        {

            label9.Text = (int.Parse(label9.Text) - 1).ToString();
            if (int.Parse(label9.Text) == 0)  //if the countdown reaches '0', we stop it

            //  MessageBox.Show("The form will now be closed.", "Time Elapsed");

            this.Close();

        }


    }
}
 
Share this answer
 
Comments
Member 14594285 31-Mar-23 11:14am    
thanks, very useful

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900