Just remove "static" attribute from OnTimerElapsed method.

Cheers!
Michał Ziemski

Panoramix pisze:
> I am doing the first steps with Mono and C #, and I stalled on a stupid
> problem.
> I implemented a timer that launches an event every X seconds.
> Now when the procedure is called I want to change a label but I can not
> because I receive this message
> "` MainWindow.label2 ': An object reference is required for the nonstatic
> field "
>
> How can I fix?
>
> // MainWindow.cs created with MonoDevelop
> // User: franaria at 11:21 15/05/2008
> //
> // To change standard headers go to Edit->Preferences->Coding->Standard
> Headers
> //
> using System;
> using System.Diagnostics;
>
> using System.IO.Ports;
>
> using System.Net;
>
> using System.Net.Sockets;
>
> using System.Threading;
> using System.Timers;     //per la gestione dei timer
> using Gtk;
> using Modbus.Device;
>
> public partial class MainWindow: Gtk.Window
> {     
>       private static System.Timers.Timer ourTimer;
>       
>       public MainWindow (): base (Gtk.WindowType.Toplevel)
>       {
>               Build ();
>               ourTimer = new System.Timers.Timer();
>
>               // attach to the Elapsed event.
>               ourTimer.Elapsed += new ElapsedEventHandler(OnTimerElapsed);
>               
>               // make our timer interval 1 second
>               ourTimer.Interval = 1000;
>       //Build ();             
>       }
>       
>       protected void OnDeleteEvent (object sender, DeleteEventArgs a)
>       {
>               Application.Quit ();
>               a.RetVal = true;
>       }
>
>       protected virtual void Butt1Start (object sender, System.EventArgs e)
>       {
>       }
>
>       protected virtual void OnButton2Clicked (object sender, 
> System.EventArgs e)
>       {
>               if (ourTimer.Enabled == false)
>               {
>                       ourTimer.Enabled = true ;
>                       button2.Label = "Starting read";
>                       }else 
>               {       
>                       ourTimer.Enabled = false ;
>                       button2.Label = "Start Timed read";
>                       }
>       }
>       
>        private static void OnTimerElapsed(object sender, ElapsedEventArgs e)
>      {
>               Console.WriteLine("Timer elapsed at: " + 
> e.SignalTime.ToLongTimeString());
>               //label2.Text = "Timer elapsed at: " + 
> e.SignalTime.ToLongTimeString();    
> <---  Problem
>               //button2.Label = "Running Timed Read";                         
>                         
> <---
>      }
>
> }
>
> Thanks to all :blush:
>
> -----
> :working:
> -----------------------------------------------------------------------------
> http://freeflow.awardspace.com http://freeflow.awardspace.com 
> -----------------------------------------------------------------------------
>
>   

_______________________________________________
Mono-list maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-list

Reply via email to