Solved

Timer To Refresh Form - C#

Posted on 2006-11-21
8
6,195 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

813 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

16 Experts available now in Live!

Get 1:1 Help Now