Click here to Skip to main content
Sign Up to vote bad
good
See more: C#2.0GUIClass
Hi,
 
I have a problem accessing the UI to provide some data which are distributed inside a class.
 
My program actually enables and disables el. power of other pc's to do some shutdown/restart (sometimes necessary when you're in tech. support, with associated HW of course).
 
I have realized that in a array of classes used for the external devices.
This class contains a timer which gives timed power on/off orders.
 
namespace SW_Bootmachine
{
    public partial class Form1 : Form
    {
        public class Device
        {
            // other code...

            Timer t1 = new Timer();
 
            void t1_Tick(object sender, EventArgs e)
            {
                // power on device
                SusiIOWriteEx(byPinNr, true);     // enable ext. power
                t1.Interval = iOnTime * 1000;
                t1.Stop();
                t1.Start();
                bPowerON = true;
 
                // write current time into a label
            }
        }
 
        public Device[] Devices;
 
At the point // write current time into a label I'd like to write the current time for Device 1 into a label called lblLastBootDev1 of Form1, to tell the user when the system was started the last time (this ofc also needs to be done for the other 7 devices)
 
Usually you can access the labels in your program code by just naming them, but inside my class this is not possible.
 
Could somebody tell me the best way to fill labels of Form1 with data of my class?
Posted 26 Jun '12 - 22:45

Comments
Wes Aday - 27 Jun '12 - 9:14
What do you mean by "Usually you can access the labels in your program code by just naming them, but inside my class this is not possible"? Why is this possible for everyone else but not you?
Wes Aday - 27 Jun '12 - 10:39
You have a couple of different options. Pass the Form1 instance to your devices class, pass the labels to your devices class or use delegate/events to update the labels.

2 solutions

Hello.
 
You can do this by using event handlers.
 
First create a class to store the event data you want to transfer:
public class TimerEventArgs : EventArgs
{
    public string TickTime;
}
 
Then in your Device class add a EventHandler:
public event EventHandler<timereventargs> OnTick;</timereventargs>
 
And use it to notify an update:
if (OnTick != null)
{
    OnTick(this, new TimerEventArgs() {TickTime = DateTime.Now.ToLongTimeString()});
}
 
All that's left to do is to use the event handler, so in you form when you create your Device instance assign it an handler:
Devices[1].OnTick += new EventHandler<timereventargs>(Form1_OnTick);
</timereventargs>
 
And when the handler is fired up just update the label you want:
 
void Form1_OnTick(object sender, TimerEventArgs e)
{
    if (sender == Devices[0])
    {
        label1.Text = e.TickTime;
    }
    if (sender == Devices[1])
    {
        label2.Text = e.TickTime;
    }
}
 
Here is a complete code sample:
 
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        Devices = new Device[2];
        Devices[0] = new Device();
        Devices[1] = new Device();
        Devices[0].OnTick += Form1_OnTick;
        Devices[1].OnTick += Form1_OnTick;
    }
 
    void Form1_OnTick(object sender, TimerEventArgs e)
    {
        if (sender == Devices[0])
        {
            label1.Text = e.TickTime;
        }
        if (sender == Devices[1])
        {
            label2.Text = e.TickTime;
        }
    }
 
    public Device[] Devices;
}
 
public class Device
{
    public Device()
    {
        t1 = new Timer();
        t1.Interval = new Random().Next(100, 200);
        t1.Tick += new EventHandler(t1_Tick);
        t1.Start();
    }
 
    private Timer t1;
    private int iOnTime = 0;
    public event EventHandler<TimerEventArgs> OnTick;
 
    void t1_Tick (object sender, EventArgs e)
    {
        t1.Interval = new Random().Next(10, 20) * 100;
        t1.Stop();
        t1.Start();
        if (OnTick != null)
        {
            OnTick(this, new TimerEventArgs() {TickTime = DateTime.Now.ToLongTimeString()});
        }
    }
 
}
 
public class TimerEventArgs : EventArgs
{
    public string TickTime;
}
 

Valery.
  Permalink  
Comments
Frostbite_dx - 28 Jun '12 - 3:42
Thanks, this is what I was looking for :)
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 325
1 Sergey Alexandrovich Kryukov 140
2 Mohammed Hameed 123
3 Santhosh G_ 113
4 Ron Beyer 99
0 Sergey Alexandrovich Kryukov 8,286
1 OriginalGriff 6,561
2 CPallini 3,533
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 27 Jun 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid