开发者

c# Refresh a windows form

开发者 https://www.devze.com 2023-03-05 04:52 出处:网络
I have a windows f开发者_如何学运维orm which has to be refreshed automatically without using any button to refresh the form.

I have a windows f开发者_如何学运维orm 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.


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
}


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.


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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消