【解決方法】タイマー C# ビジュアル スタジオ 2015

プログラミングQA


私はWindowsフォームC#を使用しましたが、30秒後にダイアログを閉じるタイマーを入れたい..

this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();

私は主に持っています:

<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);

           

        }

      
    }
}

私が試したこと:

このコードに時間を追加する方法がわかりません

解決策 1

このようにタイマーを設定する必要があります。
グーグルは「タイマーを使用してウィンドウフォームを閉じる」という多くの結果を生み出しました

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();

        }


    }
}

コメント

タイトルとURLをコピーしました