Thread: updating a label with a timer event

  1. #1
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    updating a label with a timer event

    hi ppl

    setup < windows xp , microsoft c# .NET >

    im trying to update a label on a form without any button click (or any user event) , the event that should update the label is a timer in a seperate class from the label, the code follows:

    Code:
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    
    
    namespace TrafficControlSim
    {
    	// Summary description for Form1.
    	public class Form1 : System.Windows.Forms.Form
    	{
    		private System.Windows.Forms.Label label1;
    		// Required designer variable.
    		private System.ComponentModel.Container components = null;
    		private TrafficFlowSimulator trafficflowsim;
    		private static int num;
    
    
    		public Form1()
    		{
    			// Required for Windows Form Designer support
    			InitializeComponent();
    			trafficflowsim = new TrafficFlowSimulator();
    			// TODO: Add any constructor code after InitializeComponent call
    		
    		}
    
    		// Clean up any resources being used.
    		protected override void Dispose( bool disposing )
    		{
    			if( disposing )
    			{
    				if (components != null) 
    				{
    					components.Dispose();
    				}
    			}
    			base.Dispose( disposing );
    		}
    
    		#region Windows Form Designer generated code
    		
    		// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		private void InitializeComponent()
    		{
    			this.label1 = new System.Windows.Forms.Label();
    			this.SuspendLayout();
    			// 
    			// label1
    			// 
    			this.label1.Location = new System.Drawing.Point(16, 344);
    			this.label1.Name = "label1";
    			this.label1.Size = new System.Drawing.Size(120, 56);
    			this.label1.TabIndex = 0;
    			this.label1.Text = "THIS IS THE OBJECT I WANNA UPDATE";
    			// 
    			// Form1
    			// 
    			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    			this.ClientSize = new System.Drawing.Size(592, 516);
    			this.Controls.Add(this.label1);
    			this.Name = "Form1";
    			this.Text = "Traffic Control Simulator";
    			this.Load += new System.EventHandler(this.Form1_Load);
    			this.ResumeLayout(false);
    
    		}
    		#endregion
    
    		
    		
    		public void displaytext_timer(String a)
    		{
    		
    		
    		}
    		
    		// The main entry point for the application.
    		[STAThread]
    		static void Main() 
    		{
    			
    			TrafficFlowSimulator trafficSimulator = new TrafficFlowSimulator("H:\\NorthTraffic.txt",
    				"H:\\SouthTraffic.txt");
    			
    			Application.Run(new Form1());
    			
    			
    		}
    
    		public void Form1_Load(object sender, System.EventArgs e)
    		{
    			// convert int to string
    		    num = trafficflowsim.getTimeIndex();
    			num.ToString();
    			label1.Text = "num"; 
    		}
    
    		
    
    		
    	}//end Form1 class
    
    }//end namespace
    and the other class is:

    Code:
    using System;
    using System.IO;
    //using System.Threading;
    using System.Timers;
    using System.Collections;
    
    namespace TrafficControlSim
    {
    	public class TrafficFlowSimulator
    	{
    		private  System.Timers.Timer oneSecondTimer; 
    		private  TrafficQueue northQueue,southQueue;
    		private  TrafficSensor northSensor,southSensor;
    		private  static int timeIndex;
    		private  Random random;
    		private string fileText;
    		private Form1 form;
    		 
    	
    
    		public TrafficFlowSimulator(string north, string south)
    		{
    			try
    			{
    				northSensor = new TrafficSensor(north);
    				southSensor = new TrafficSensor(south);
    				oneSecondTimer= new System.Timers.Timer();
    				northQueue = new TrafficQueue(15);
    				southQueue = new TrafficQueue(15);
    				oneSecondTimer.Elapsed += new ElapsedEventHandler(OnOneSecond);
    				oneSecondTimer.Interval = 1000;
    				oneSecondTimer.Enabled = true;
    				fileText = "queue";
    				timeIndex = 0;
    				random = new Random();
    				form = new Form1();
    				
    				
    			}
    			catch (Exception e)
    			{
    				Console.WriteLine("Could not initialise traffic simulator. "+e);
    			}
    		}
    
    		public TrafficFlowSimulator()
    		{
    		}
    
    		public void OnOneSecond(Object source, ElapsedEventArgs e)
    		{
    			timeIndex++;
    			if (timeIndex%7==0)
    				CheckTrafficSensors();
    			Console.WriteLine("Sim time: " + timeIndex + " seconds");
    			form.Form1_Load(source, e);
    			if (timeIndex==120)
    			{
    				Console.WriteLine("Press q to quit");
    				oneSecondTimer.Enabled=false;
    				oneSecondTimer.Dispose();
    			}
    			else
    			{
    				Console.WriteLine("Traffic Queue North has "+northQueue.TrafficQueueSize() +" vehicles in, namely");
    				northQueue.TrafficInQueue();
    				Console.WriteLine();
    				
    				Console.WriteLine("Traffic Queue South has "+southQueue.TrafficQueueSize() +" vehicles in, namely");
    				southQueue.TrafficInQueue();
    				Console.WriteLine();
    			}
    		}
    
    		public void CheckTrafficSensors()
    		{
    			fileText = northSensor.ReadTrafficSensor();
    			if (fileText.Equals("car"))
    			{
    				Car aCar = new Car();
    				aCar.SetVehicleType("car");
    				northQueue.Arrive(aCar);
    			}
    			if (fileText.Equals("lorry"))
    			{
    				Lorry aLorry = new Lorry();
    				aLorry.SetVehicleType("Lorry");
    				northQueue.Arrive(aLorry);
    			}
    			fileText = southSensor.ReadTrafficSensor();
    			if (random.NextDouble()<0.5)
    			{
    				if (fileText.Equals("car"))
    				{
    					Car aCar = new Car();
    					aCar.SetVehicleType("car");
    					southQueue.Arrive(aCar);
    				}
    				if (fileText.Equals("lorry"))
    				{
    					Lorry aLorry = new Lorry();
    					aLorry.SetVehicleType("Lorry");
    					southQueue.Arrive(aLorry);
    				}		
    			}
    	
    	
    		}// end CheckTrafficSensors
    
    		public int getTimeIndex()
    		{
    			return timeIndex;
    		}
    
    	}//end class TrafficFlowSimulator
    
    }//end namespace
    the Form1_Load does get called on every iteration of the timer but the form label never changes from the 1st text label ..ie num

    thanks in advance

    luigi

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    Code:
    			label1.Text = "num"; 
    		}
    U might want to change this to

    label1.Text = ""+num;
    Last edited by GanglyLamb; 03-30-2005 at 10:18 AM.

  3. #3
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61
    yes i see what you mean, i was still output the string, however the label doesnt update on every iteration, i tried the label1.Invalidate after label1.Text = ""+num; to no avail

  4. #4
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,110
    IŽd suggest using the debugging mode of visual .Net, so you can really see whats going on where.

    I saw as well after compiling it, that it just outputs 0 constantly, though i cant help you further since ive been looking in this time handler thing as well and i couldnt figure out how it was exactly working ...

  5. #5
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61
    yes i just ran the program, placed a bp at

    label1.Text = ""+num;

    statement at which point num increments by one on every timer iteration, the int just isnt being passed to the label1 object

    im going read around a little

    i would be grateful for any feedback

    tahnks in advance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. continuously updating timer on console display
    By revelation437 in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2003, 12:28 PM