3

我有一个带有标签的表单,名为labelTime. 在另一个名为我的类中,TimeCalculate我有一个TimerwithTimer_tick事件处理程序。在这个类中,我还有一个GetTime()以字符串格式返回时间的函数。我希望这个字符串出现在labelTime每个Timer_tick. 有没有办法做到这一点?

public void MyTimer(Label o_TimeLabel) 
{
  Timer Clock = new Timer(); 
  Clock.Tag = o_TimeLabel.Text; 
  Clock.Interval = 1000; Clock.Start(); 
  Clock.Tick += new EventHandler(Timer_Tick); 
} 

private void Timer_Tick(object sender, EventArgs eArgs) 
{ 
  if (sender == Clock) 
  { 
    //LabelTime.Text = GetTime(); <-- I want this to work! 
  } 
}
4

3 回答 3

1

TimerandTimer_Tick事件不需要和你的 同一个类,Label你可以创建一个简单的自定义事件来发布/订阅你的Timer_Tick event.

你的TimeCalculate班级:

namespace StackOverflow.WinForms
{
    using System;
    using System.Windows.Forms;

    public class TimeCalculate
    {
        private Timer timer;

        private string theTime;
        public string TheTime
        {
            get
            {
                return theTime;
            }
            set
            {
                theTime = value;
                OnTheTimeChanged(this.theTime);
            }
        }

        public TimeCalculate()
        {
            timer = new Timer();
            timer.Tick += new EventHandler(Timer_Tick);
            timer.Interval = 1000;
            timer.Start();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            TheTime = DateTime.UtcNow.ToString("dd/mm/yyyy HH:mm:ss");
        }

        public delegate void TimerTickHandler(string newTime);
        public event TimerTickHandler TheTimeChanged;

        protected void OnTheTimeChanged(string newTime)
        {
            if (TheTimeChanged != null)
            {
                TheTimeChanged(newTime);
            }
        }
    }
}

上面极其简化的示例展示了如何在对象事件触发时使用 adelegateeventtopublish通知。Timer_Tick Timer

Timer_Tick事件触发时需要通知的任何对象(即您的时间已更新)只需要subscribe您的自定义事件发布者:

namespace StackOverflow.WinForms
{
    using System.Windows.Forms;

    public partial class Form1 : Form
    {
        private TimeCalculate timeCalculate;

        public Form1()
        {
            InitializeComponent();
            this.timeCalculate = new TimeCalculate();
            this.timeCalculate.TheTimeChanged += new TimeCalculate.TimerTickHandler(TimeHasChanged);

        }

        protected void TimeHasChanged(string newTime)
        {
            this.txtTheTime.Text = newTime;
        }
    }
}

TimeCalcualte我们在订阅事件之前创建一个类的实例,TimerTickHandler指定一个方法(TimeHasChanged)来处理通知。请注意,这是我在表格中txtTheTime给出的名称。TextBox

于 2013-07-19T13:59:24.853 回答
1

您需要与标签形式相同的计时器及其事件

编辑

既然你已经这样做了,你需要;

将 Timer 对象声明为实例变量,在构造函数之外,在构造函数中对其进行初始化。
不必担心测试'sender == Clock'。
并且在此类中也有一个 Label 实例对象,将其设置为您在构造函数中作为参数传递的 Label。

    Timer Clock;
    Label LabelTime;
    public void MyTimer(Label o_TimeLabel) 
    {
      LabelTime = o_TimeLabel;
      Clock = new Timer(); 
      Clock.Tag = o_TimeLabel.Text; 
      Clock.Interval = 1000;
      Clock.Start(); 
      Clock.Tick += new EventHandler(Timer_Tick); 
    } 

    private void Timer_Tick(object sender, EventArgs eArgs) 
    { 
      LabelTime.Text = GetTime(); // For your custom time
    }
于 2013-07-19T12:47:23.397 回答
1

Rebecca 在您的 Time_Tick 事件中,您可以执行以下操作

private void Timer_Tick(object sender, EventArgs e)
{      
  lblTime.Text = DateTime.Now.ToString("hh:mm:ss");
}
于 2013-07-19T12:48:18.690 回答