How can I use the method ChangeText
in my static method timer_Elapsed
?
public Load()
{
InitializeComponent();
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = 1000;
// I can't transfer parameters here
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Start();
}
static void timer_Elapsed(object sender, ElapsedEventArgs e)
{
//Its underlined in red. I need a object reference?
ChangeText();
}
public void ChangeText()
{
label1.Text = label1.Text + ".";
}
As first, your method (timer_Elapsed) could not me static, in order to use an instance property (label1)
There is an other problem in your code: Timer create an other thread, an most of windows control properties can be modified only by UI thread. Your code will throw a CrossThreadException. In order to resolve your problem , you should modify your code with this:
if(this.InvokeRequired) {
BeginInvoke(
new MethodInvoker(delegate { label.Text+="."; }));
} else {
label.Text+=".";
}
Regards