Hack Forums
[.NET] [C#] on form load start timer - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] [C#] on form load start timer (/showthread.php?tid=5948656)



[C#] on form load start timer - Formula 1 - Mar 8, 2019

How can i make a timer when form loaded start timer

00:00:00
hh/mm/ss

also 1 Label that shows the exact time of form load

Example:

if the current time is : 20:30

The program should work like that:

Form loaded at: 20:30

This timer starts -----> Timer started: 00:00:00


RE: [C#] on form load start timer - NYANxCAT - Mar 8, 2019

https://www.dotnetperls.com/datetime-now

example:
this.Text = DateTime.Now.ToLongTimeString();


RE: [C#] on form load start timer - itcorp - Mar 8, 2019

(Mar 8, 2019 01:33 PM)uberhacker Wrote:  How can i make a timer when form loaded start timer

00:00:00
hh/mm/ss

also 1 Label that shows the exact time of form load

Example:

if the current time is : 20:30

The program should work like that:

Form loaded at: 20:30

This timer starts ----->   Timer started: 00:00:00

Code
var form2 = new Form2();
form2.SetLoadTime.Text = DateTime.Now.ToString("hh:mm");
form2.Show();
Code
public partial class Form2 : Form
    {
        Timer counter;
        public Label SetLoadTime
        {
            get { return label1; }
            set { label1 = value; }
        }

        public Label SetTimeAfterLoad
        {
            get { return label2; }
            set { label2 = value; }
        }

        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            counter = new Timer();
            counter.Interval =500;
            counter.Tick += Counter_Tick;
            counter.Start();

        }

        private void Counter_Tick(object sender, EventArgs e)
        {
            SetTimeAfterLoad.Text= DateTime.Now.ToString("hh:mm:ss");
        }
    }



RE: [C#] on form load start timer - Suspect. - Mar 8, 2019

Why use a Timer for this?
Why not just call the method which appends the text to the label when it’s needed rather than running it on an endless loop?