Solved

Timer To Refresh Form - C#

Posted on 2006-11-21
8
6,241 Views
Last Modified: 2007-12-19
Hey guys,

I am after code for a timer, I need it to be running in the background and every 20 seconds refresh a form so that a textBox can be updated.

It will be updated by a function that will contain all the details of what has to go in it.  So just looking for something like this:

if timer = 20 seconds
refresh Form1
0
Comment
Question by:angus_young_acdc
  • 4
  • 3
8 Comments
 
LVL 8

Expert Comment

by:sabeesh
ID: 17985910
you can place global timer and set timer period 20 and call the function from timer_tick events.
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 17985981

//declare member
Timer _RefreshFormTimer = new Timer();

//In constructor of form
{
   ...
   _RefreshFormTimer.Interval = 1000 * 20; //20 seconds.
   _RefreshFormTimer.Tick += new EventHandler(RefreshFormTimer_Tick);
   ...
}

//Then the handler:
private void RefreshFormTimer_Tick(object sender, EventArgs e)
{
    //Refresh the form.
}
0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 18020389
Cant get that to work mate,  can you tell me more about where to put everything?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Accepted Solution

by:
WinterMuteUK earned 50 total points
ID: 18020501
Whereabouts is the problem?
Do you get errors?

The below 'skeleton' class should show where things go...

Cheers

Wint

public class MyForm : Form
{
   //declare member
   Timer _RefreshFormTimer = new Timer();

   //In constructor of form
   public MyForm()
   {
      InitializeComponent();
      _RefreshFormTimer.Interval = 1000 * 20; //20 seconds.
      _RefreshFormTimer.Tick += new EventHandler(RefreshFormTimer_Tick);
   }

   //Then the handler:
   private void RefreshFormTimer_Tick(object sender, EventArgs e)
   {
        //Refresh the form.
        //Here you put the code you need to refresh the form....
   }
}
0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 18026722
Hi mate,

There is no real problem per say, there are no errors or crashes.  It's just that I can't seem to get the code to do anything.  Here is the layout I am using with it:
namespace TestingTimer
{
    public partial class frmMain : Form
    {
        Timer _RefreshFormTimer = new Timer();
        public frmMain()
        {
            _RefreshFormTimer.Interval = 1000 * 20; //20 seconds.
            _RefreshFormTimer.Tick += new EventHandler(tmrRefreshForm_Tick);
        }

   private void RefreshFormTimer_Tick(object sender, EventArgs e)
   {
        MessageBox.Show("The timer works");
   }

I wanted the messagebox just so that it would show if it had worked.  All I seem to get is a blank form loading (which isnt right as the form that is meant to load is different, so the code must be creating a new one) and then nothing else.   C# isnt my greatest strength so no doubt I am making a mess of this in a really obvious way, so all help is appreciated bud.
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 18035139
ahhh
sorry, put:
_RefreshFormTime.Enabled = true;

after the .Tick bit.

Sorry!!! I should've put that there!!!

Wint.
0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 18035736
No worries mate and thanks that works brilliantly!  :D
0
 
LVL 9

Expert Comment

by:WinterMuteUK
ID: 18035753
Good to know!

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 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