Edited:
Maybe it is better to use class System.Windows.Forms.Timer
instead of System.Timers.Timer
. There you can call your function and also access your Textbox.
Otherwise you will receive an InvalidOperationException by trying to access your Textbox txt
in time_Tick
.
You don't neet a loop for incrementing your value i
. just restart your timer and set the new value. What you are doing now is waiting ONE tick (lasting 1000 ms) and then starting your loop.
For example this could be your method:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private int i = 0;
private Timer time;
public Form1()
{
InitializeComponent();
time = new Timer();
time.Tick += time_Tick;
time.Interval = 1000;
time.Start();
}
private void time_Tick(object e, EventArgs ea)
{
if (i < 100)
{
txt.Text = i.ToString();
i++;
time.Start();
}
}
}
}