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