Hier der Code zum Testen.
Gruß
MCoder
Gruß
MCoder
C#:
///////////////////////////////////////////////////////////////////////////////
// Test-Form
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
namespace timertest
{
public class TimerTest : Form
{
private Button m_StartStopButton = new Button();
private Label m_IntervalInfoLabel = new Label();
private System.Windows.Forms.Timer m_Timer = new System.Windows.Forms.Timer();
private Stopwatch m_Stopwatch = new Stopwatch();
private int m_TickNumber = 0;
public TimerTest()
{
// Oberfläche
m_StartStopButton.Location = new Point(10,10);
m_StartStopButton.Size = new Size(100,25);
m_StartStopButton.Text = "Start";
m_StartStopButton.Click += new EventHandler(OnStartStopButtonClick);
m_IntervalInfoLabel.Location = new Point(10,45);
m_IntervalInfoLabel.Size = new Size(100,25);
m_IntervalInfoLabel.BorderStyle = BorderStyle.Fixed3D;
Controls.Add(m_StartStopButton);
Controls.Add(m_IntervalInfoLabel);
Text = "TimerTest";
ClientSize = new Size(120, 80);
// Timer
m_Timer.Interval = 5000;
m_Timer.Tick += new EventHandler(OnTimerTick);
}
void OnStartStopButtonClick(object sender, EventArgs e)
{
if( m_Timer.Enabled )
{
m_Stopwatch.Stop();
m_Timer.Stop();
m_StartStopButton.Text = "Start";
m_IntervalInfoLabel.Text = "stopped";
}
else
{
m_Stopwatch.Reset();
m_Stopwatch.Start();
m_TickNumber = 0;
m_Timer.Start();
m_StartStopButton.Text = "Stop";
}
}
void OnTimerTick(object sender, EventArgs e)
{
m_IntervalInfoLabel.Text = String.Format( "({0}) {1}",
++m_TickNumber,
m_Stopwatch.ElapsedMilliseconds );
m_Stopwatch.Reset();
m_Stopwatch.Start();
}
}
}
///////////////////////////////////////////////////////////////////////////////
// Verwendung
timertest.TimerTest TimerTestForm = new timertest.TimerTest();
TimerTestForm.ShowDialog();
TimerTestForm.Dispose();