Click here to Skip to main content
15,889,096 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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.

C#
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
Comments
[no name] 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?
[no name] 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.

Hello.

You can do this by using event handlers.

First create a class to store the event data you want to transfer:
C#
public class TimerEventArgs : EventArgs
{
    public string TickTime;
}


Then in your Device class add a EventHandler:
C#
public event EventHandler<timereventargs> OnTick;</timereventargs>


And use it to notify an update:
C#
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:
C#
Devices[1].OnTick += new EventHandler<timereventargs>(Form1_OnTick);
</timereventargs>


And when the handler is fired up just update the label you want:

C#
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:

C#
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;
}


C#
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()});
        }
    }

}


C#
public class TimerEventArgs : EventArgs
{
    public string TickTime;
}



Valery.
 
Share this answer
 
Comments
Frostbite_dx 28-Jun-12 3:42am    
Thanks, this is what I was looking for :)
See this CodeProject article.Updating the UI from a thread - The simplest way[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900