Solved

How to start timers in the main form from a popup window?

Posted on 2011-09-09
3
238 Views
Last Modified: 2016-02-19
I have a program that runs an automat, and this automat is connected to various devices that customers might rent for some time. The automat is a Linux machine, so I'm using Mono to make my Visual Studio C# project work on it.

I have created a popup panel that maintenance personell etc. can access to turn devices on and off without having to pay for it. So for this problem I think it should suffice to look at two of the windows forms in the project, frmMain.cs and frmPopupPanel.cs.

frmPopupPanel.cs is started from frmMain.cs by some obscure combination of screen touches, so that regular users won't stumble upon it (I have a PIN code there as well). When I start a device from frmPopupPanel.cs, I need to start a timer that should run for as long as the maintenance person has set it to run.

In frmMain.cs I have a method UpdateDeviceStatus() that is executed every second by another timer to see if the various devices should stay turned on or be turned off. The timers I need to start when I turn on a device should thus be something like this:

int TimeToLive = //this will get the value that the maintenance person specifies
Boolean keedDeviceOn = false;

Timer deviceTimer = new Timer();
deviceTimer.interval = 1000;
deviceTimer.Tick += new Eventhandler(deviceTimer_Tick);
deviceTimer.Enabled = True;

void deviceTimer_Tick(Sender object, Eventargs e)
{
      if (TimeToLive == 0)
           keepDeviceOn = false;
      else 
      {
            keepDeviceOn = true;
            TimeToLive--;
      }
}

Open in new window


Now to the problem. As far as I can see, these timers (one for every device) must be created in frmMain.cs, so that UpdateDeviceStatus can check the boolean value keepDeviceOn for every device when it runs. On the other hand, I need to start the timer from frmPopupPanel.cs when the maintenance person clicks a button there.

I am at loss as to how I should do this. What I've read about timers says that they are started when they are created, so how can I make this work in my program?

I tried setting the parent of frmPopupPanel.cs to frmMain.cs, but it crashed when I tried to run that on the Linux machine, so it appears I cannot use the parent property.
0
Comment
Question by:webuser9632
3 Comments
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 125 total points
ID: 36508712
In the popup form have a reference to the main form.  This has a public function that can be called to create the timers.

0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 36510449
"I tried setting the parent of frmPopupPanel.cs to frmMain.cs, but it crashed"

How did you do that?

Pass in "this" to your instance of frmPopupPanel when you Show() it:

    frmPopupPanel popup = new frmPopupPanel();
    popup.Show(this); // <-- set the owner

Now, in "popup", you can cast that back to frmMain via the Owner() property:

    // ... in frmPopupPanel ...
    frmMain main = (frmMain)this.Owner;
    main.Text = "Hello from Popup!";

If that doesn't work then you'll need to manually pass in a reference as Andy proposes.  Let us know if that is the case and you need help setting that up.
0
 

Author Closing Comment

by:webuser9632
ID: 36585306
Sorry for being late guys. I thought I closed this question last week, but I must have forgotten to press some button.

You both more or less gave me the solution. However, the syntax that Idle_Mind suggested caused an error when I ran it in Linux. I had to do it this way:
I let the constructor in frmPopupPanel take a frmMain as parameter, then I wrote this line in frmMain:
frmPopupPanel _Popup = new frmPopupPanel(this);
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now