Solved

Timer To Refresh Form - C#

Posted on 2006-11-21
8
6,136 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
Comment Utility
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
Comment Utility

//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
Comment Utility
Cant get that to work mate,  can you tell me more about where to put everything?
0
 
LVL 9

Accepted Solution

by:
WinterMuteUK earned 50 total points
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 15

Author Comment

by:angus_young_acdc
Comment Utility
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
Comment Utility
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
Comment Utility
No worries mate and thanks that works brilliantly!  :D
0
 
LVL 9

Expert Comment

by:WinterMuteUK
Comment Utility
Good to know!

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

6 Experts available now in Live!

Get 1:1 Help Now