c# 刷新窗口窗体

本文关键字:窗体 窗口 刷新 | 更新日期: 2023-09-27 17:56:11

>我有一个窗口表单,它必须自动刷新,而无需使用任何按钮来刷新表单。

现在正在使用一个按钮来刷新表单。但是我需要表单每 1 分钟自动刷新一次。

可以在窗口表单应用程序中进行操作。

c# 刷新窗口窗体

我不确定为什么需要刷新表单,但是将按钮后面的任何代码放在计时器事件中。 您已经有了代码,因此只需创建一个计时器,将其设置为所需的长度,然后将其打开即可。

以下是您需要的代码:

  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
}

您可以向表单添加计时器并在Form_Load启用它。将计时器值(以毫秒为单位)设置为 60000。在Timer_Tick函数中,您可以放置用于刷新的代码。

使用System.Windows.Forms.Timer。

计时器.Tick 事件 在指定的计时器间隔已过并启用计时器时发生。您可以使用它来刷新表单。

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

使用 Timer.Interval 属性指定计时器间隔。在您的情况下,您需要将其设置为 60,000:

Timer.Interval = 60000;

这些是关于它的一些教程:

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

http://www.dotnetperls.com/timer

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

使用计时器控件并将间隔设置为 60*1000 毫秒(1 分钟),并在 tick 事件中使用代码刷新窗体。

做这些工作!循序渐进:

  1. 向表单添加计时器
  2. 将值(间隔)设置为 1000
  3. 双击表单
  4. 键入以下内容Form_Load:

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

  5. 双击计时器并键入以下内容timer_tick:

    this.Refresh();