3

というラベルが付いたフォームがありlabelTimeます。と呼ばれる別のクラスTimeCalculateには、TimerwithTimer_tickイベント ハンドラーがあります。このクラスにGetTime()は、時刻を文字列形式で返す関数もあります。labelTimeこの文字列をfor everyに表示したい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

ラベルと同じ形式でタイマーとそのイベントが必要です

編集

そのようにしたので、次のことを行う必要があります。

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

TimerおよびTimer_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);
            }
        }
    }
}

上記の非常に単純化された例は、オブジェクト イベントが発生したときに通知にdelegateandeventを使用する方法を示しています。publishTimer_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してイベントをサブスクライブする前に、クラスのインスタンスを作成します。フォームに付けた名前であることに注意してください。TimerTickHandlerTimeHasChangedtxtTheTimeTextBox

于 2013-07-19T13:59:24.853 に答える