Update function to update date and time?

2
I have a Windows Form , in the control bar (where you have the Title, close and minimize) I also have the date and time:

        this.hora.Text = DateTime.Now.ToShortTimeString();
        this.data.Text = DateTime.Now.ToShortDateString();

But you are not updating by yourself, I tried to get the mouse movement and the key pressed, but you can not call the functions that execute this all the time .. I do not know, I have no idea how to do this, I had only seen C # in Unity and there was the ready function Update that did this role (kept updating itself and executing the commands inside it), but how do I do this in Visual ?

I'm calling it like this:

 public Interface()
    {
        InitializeComponent();
        KeyPress += _keyPress;
        MouseMove += _mouseMove;

    }
    public void _mouseMove(object sender, MouseEventArgs e)
    {
        this.mouseLocation = e.Location;
        this.hora.Text = DateTime.Now.ToShortTimeString();
        this.data.Text = DateTime.Now.ToShortDateString();
    }
    public void _keyPress(object sender, KeyPressEventArgs e)
    {
        this.hora.Text = DateTime.Now.ToShortTimeString();
        this.data.Text = DateTime.Now.ToShortDateString();
    }
    
asked by anonymous 02.07.2015 / 21:32

2 answers

0

First, declare this timer:

 public Timer WithEventsnowUpdater = new Timer();

Now put this method:

 public override void OnLoad() {
      nowUpdater.Enabled = true;
      nowUpdater.Interval = 1000; /*1 segundo = 1.000*/
      nowUpdater.Tick += onLoad();
 } //ou coloque essas duas linhas no método Form1_Load()

 public void nowTick() {
      this.hora.Text = DateTime.Now.ToShortTimeString();
      this.data.Text = DateTime.Now.ToShortDateString();
 }
    
13.08.2015 / 21:52
0

You need to use a timer, so you can update the date and / or time within a certain time frame at your discretion. See Here Ways !)

    
13.08.2015 / 21:44