Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 GUI
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 23:45pm
Comments
Wes Aday at 27-Jun-12 9:14am
   
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 at 27-Jun-12 10:39am
   
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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 at 28-Jun-12 3:42am
   
Thanks, this is what I was looking for :)

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

  Print Answers RSS
0 OriginalGriff 375
1 Sergey Alexandrovich Kryukov 329
2 CPallini 260
3 DamithSL 214
4 Maciej Los 197
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 27 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100