c# windows窗体的秒表,有3个pc's,它们都不知道为什么一起启动-(编码新手)
本文关键字:为什么 一起 不知道 启动 新手 编码 窗体 windows 3个 pc | 更新日期: 2023-09-27 18:15:43
using System;
using System.Windows.Forms;
namespace Stopwatch
{
public partial class StopwatchSpikeForm : Form
{
private Timer _timer, _timerB, _timerC;
private DateTime _startTime = DateTime.MinValue;
private DateTime _startTimeB = DateTime.MinValue;
private DateTime _startTimeC = DateTime.MinValue;
private TimeSpan _currentElapsedTime = TimeSpan.Zero;
private TimeSpan _currentElapsedTimeB = TimeSpan.Zero;
private TimeSpan _currentElapsedTimeC = TimeSpan.Zero;
private TimeSpan _totalElapsedTime = TimeSpan.Zero;
private TimeSpan _totalElapsedTimeB = TimeSpan.Zero;
private TimeSpan _totalElapsedTimeC = TimeSpan.Zero;
private bool _timerRunning = false;
private bool _timerRunningB = false;
private bool _timerRunningC = false;
public StopwatchSpikeForm()
{
InitializeComponent();
_timer = _timerB = _timerC = new Timer();
_timer.Interval = _timerB.Interval = _timerC.Interval = 1000;
_timer.Tick += new EventHandler(_timer_Tick);
_timerB.Tick += new EventHandler(_timerB_Tick);
_timerC.Tick += new EventHandler(_timerC_Tick);
}
void _timer_Tick(object sender, EventArgs e)
{
var timeSinceStartTime = DateTime.Now - _startTime;
timeSinceStartTime = new TimeSpan(timeSinceStartTime.Hours,
timeSinceStartTime.Minutes,
timeSinceStartTime.Seconds);
_currentElapsedTime = timeSinceStartTime + _totalElapsedTime;
_totalElapsedTimeDisplay.Text = _currentElapsedTime.ToString();
_currentElapsedTimeDisplay.Text = timeSinceStartTime.ToString();
}
#region PC
private void startButton_Click(object sender, EventArgs e)
{
if (!_timerRunning)
{
_startTime = DateTime.Now;
_totalElapsedTime = _currentElapsedTime;
_timer.Start();
_timerRunning = true;
}
else
{
_timer.Stop();
_timerRunning = false;
}
}
private void resetButton_Click(object sender, EventArgs e)
{
_timer.Stop();
_timerRunning = false;
_totalElapsedTime = TimeSpan.Zero;
_currentElapsedTime = TimeSpan.Zero;
}
#endregion
#region PC-B
void _timerB_Tick(object sender, EventArgs e)
{
var timeSinceStartTimeB = DateTime.Now - _startTimeB;
timeSinceStartTimeB = new TimeSpan(timeSinceStartTimeB.Hours,
timeSinceStartTimeB.Minutes,
timeSinceStartTimeB.Seconds);
_currentElapsedTimeB = timeSinceStartTimeB + _totalElapsedTimeB;
_totalElapsedTimeDisplayB.Text = _currentElapsedTimeB.ToString();
_currentElapsedTimeDisplayB.Text = timeSinceStartTimeB.ToString();
}
private void startButtonB_Click(object sender, EventArgs e)
{
if (!_timerRunningB)
{
_startTimeB = DateTime.Now;
_totalElapsedTimeB = _currentElapsedTimeB;
_timerB.Start();
_timerRunningB = true;
}
else
{
_timerB.Stop();
_timerRunningB = false;
}
}
private void resetButtonB_Click(object sender, EventArgs e)
{
_timerB.Stop();
_timerRunningB = false;
_totalElapsedTimeB = TimeSpan.Zero;
_currentElapsedTimeB = TimeSpan.Zero;
}
#endregion
#region PC-C
void _timerC_Tick(object sender, EventArgs e)
{
var timeSinceStartTimeC = DateTime.Now - _startTimeC;
timeSinceStartTimeC = new TimeSpan(timeSinceStartTimeC.Hours,
timeSinceStartTimeC.Minutes,
timeSinceStartTimeC.Seconds);
_currentElapsedTimeC = timeSinceStartTimeC + _totalElapsedTimeC;
_totalElapsedTimeDisplayC.Text = _currentElapsedTimeC.ToString();
_currentElapsedTimeDisplayC.Text = timeSinceStartTimeC.ToString();
}
private void startButtonC_Click(object sender, EventArgs e)
{
if (!_timerRunningC)
{
_startTimeC = DateTime.Now;
_totalElapsedTimeC = _currentElapsedTimeC;
_timerC.Start();
_timerRunningC = true;
}
else
{
_timerC.Stop();
_timerRunningC = false;
}
}
private void resetButtonC_Click(object sender, EventArgs e)
{
_timerC.Stop();
_timerRunningC = false;
_totalElapsedTimeC = TimeSpan.Zero;
_currentElapsedTimeC = TimeSpan.Zero;
}
#endregion
}
}
创建一个定时器,并将其分配给_timer
, _timerB
和_timerC
。
代替:
_timer = _timerB = _timerC = new Timer();
:
_timer = new Timer();
_timerB = new Timer();
_timerC = new Timer();