7

I have a windows form which has to be refreshed automatically without using any button to refresh the form.

Right now am using a button to refresh the form. But I need the form to refresh automatically for every 1 minute.

It is possible to do in windows form application.

Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
bharathi
  • 6,019
  • 23
  • 90
  • 152
  • Refer http://stackoverflow.com/questions/5396282/auto-refresh-in-asp-net-mvc for asp.net or refer http://stackoverflow.com/questions/5371857/wpf-auto-refresh-combobox-content for wpf. If you want to customize you can use timer to trigger autorefresh – Sandeep G B May 10 '11 at 05:29

5 Answers5

5

I'm not sure why you need to refresh a form, but put whatever code you have behind the button in a timer event. You already have the code, so just create a timer, set it for the length you want, and turn it on.

Here is the code you need:

  Timer myTimer = new Timer();
  myTimer.Elapsed += new ElapsedEventHandler( TimeUp );
  myTimer.Interval = 1000;
  myTimer.Start();

public static void TimeUp( object source, ElapsedEventArgs e )
{
    //Your code here
}
IAmTimCorey
  • 16,412
  • 5
  • 39
  • 75
  • 2
    I think what the OP meant was to refresh some data on a form. – tzup May 10 '11 at 05:43
  • Do not recommend System.Timers.Timer since it hides errors (all unhandled exceptions will silently be ignored). System.Threading.Timer From MSDN: In the .NET Framework version 2.0 and earlier, the Timer component catches and suppresses all exceptions thrown by event handlers for the Elapsed event. This behavior is subject to change in future releases of the .NET Framework. It does not say if it have been changed yet or not. – Pankaj May 10 '11 at 05:44
  • Thanks for your answer.Since this is my first project i do where to paste the first four line of your code.I have to paste within the constructor of the form.please help me. – bharathi May 10 '11 at 06:10
  • @bharathi - The first four lines need to be in some method. Usually it is in the Form_Load method or whatever method is running when the form starts up. – IAmTimCorey May 10 '11 at 06:16
  • I have add the first four lines to the load form.within the public static void TimeUp(object source,ElapsedEventArgs e){ getdata();}.In this getdata will get the data from the database.Once add this getdata method within Time up is says Error 1 An object reference is required for the non-static field, method, or property '.Even i tried By removing static it is not working. – bharathi May 10 '11 at 06:35
  • I have created a object and i called the getdata method.But still it is not working – bharathi May 10 '11 at 06:46
2

You can add a Timer to the form and enable it on Form_Load. Set the timer value in milliseconds to 60000. In the Timer_Tick function, you can put the code meant for refreshing.

Khemka
  • 108
  • 2
  • 9
2

Use System.Windows.Forms.Timer.

The Timer.Tick event Occurs when the specified timer interval has elapsed and the timer is enabled. You can use it to refresh your form.

 // This is the method to run when the timer is raised.
private static void Timer_Tick(Object myObject, EventArgs myEventArgs) 
{ // Refresh Form }

Use the Timer.Interval property to specify the timer interval. In your case you need to set it to 60,000:

Timer.Interval = 60000;

Those are some tutorials about it:

http://www.codeproject.com/KB/cs/timeralarm.aspx

http://www.dotnetperls.com/timer

http://www.c-sharpcorner.com/UploadFile/mahesh/WorkingwithTimerControlinCSharp11302005054911AM/WorkingwithTimerControlinCSharp.aspx

Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
1

Use a Timer control and in set the Interval as 60*1000 ms(1 min) and in the tick event use the code to refresh the Form.

Anuraj
  • 18,859
  • 7
  • 53
  • 79
0

Do these works! Step by Step:

  1. Add a timer to your Form
  2. Set the value(Interval) to 1000
  3. Double click on Form
  4. Type this for Form_Load:

    timer1.Start(); //Set your timer name instead of "timer1"

  5. Double click on timer and type this for timer_tick:

    this.Refresh();

Pouya
  • 109
  • 1
  • 8