bharathi
bharathi

Reputation: 6271

c# Refresh a windows form

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.

Upvotes: 7

Views: 32485

Answers (5)

Pouya
Pouya

Reputation: 109

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();

Upvotes: 0

Akram Shahda
Akram Shahda

Reputation: 14771

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

Upvotes: 2

Khemka
Khemka

Reputation: 108

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.

Upvotes: 2

IAmTimCorey
IAmTimCorey

Reputation: 16757

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
}

Upvotes: 5

Anuraj
Anuraj

Reputation: 19598

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.

Upvotes: 1

Related Questions