Solved

Timer To Refresh Form - C#

Posted on 2006-11-21
8
6,264 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Suggested Courses

734 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