递增/递减时间
Increment / Decrement Time
您好我在尝试制作一个简单的事件调度程序时遇到了一些问题。
在下面的代码中,我的目的是增加或减少时间(当使用鼠标事件按下按钮时)。
但是试了很多次都搞不明白为什么能增加;但不是减少时间。
// Timer
private Timer TmrCount;
private int HH {get;set;}
private int MM { get; set; }
private Button CurrBtn = new Button();
#region <Mouse Events>
private void OnMouseDown(object sender, EventArgs e)
{
CurrBtn = (Button)sender;
StartTimer();
}
private void OnMouseUp(object sender, MouseEventArgs e)
{
StopTimer();
}
#endregion
#region <Timer>
private void StartTimer()
{
if (TmrCount == null)
{
TmrCount = new Timer();
TmrCount.Interval = 210;
TmrCount.Tick += TmrCount_Tick;
TmrCount.Start();
}
}
private void TmrCount_Tick(object sender, EventArgs e)
{
Set_Time();
}
private void StopTimer()
{
if (TmrCount != null)
{
TmrCount.Stop();
TmrCount.Dispose();
TmrCount.Tick -= TmrCount_Tick;
TmrCount = null;
}
}
private void Set_Time()
{
switch (CurrBtn.Text)
{
case "+":
// Condition Check (Increase HH))
//if (HH == 23) { HH = default(int); }
//Increase HH
//if (HH < 23) { HH += 1; }
while (HH < 23) { HH++; break; }
break;
case "-":
// Condition Check (Decrease HH)
//if (HH == default(int)) { HH = 23; }
//if (HH == 0) { HH = 23; }
// Decrease HH
while (HH > 23) { HH-=1; break; }
break;
}
// Set Hour Text into Label
lbl_HH.Text = Convert.ToString(HH);
}
#endregion
谁能给我指出正确的方向?
提前致谢
假设您的 HH 变量以值 1 开头,那么您应该以这种方式更改您的 SetTime
int HH = 1;
private void Set_Time()
{
switch (CurrBtn.Text)
{
case "+":
// Condition Check (Increase HH))
// Within a limit of 23
while (HH < 23) { HH++; break; }
break;
case "-":
// Condition Check (Decrease HH)
// Decrease HH but don't allow it to be less than 0
while (HH >= 0) { HH-=1; break; }
break;
}
// Set Hour Text into Label
lbl_HH.Text = Convert.ToString(HH);
}
顺便说一句,在处置一个对象后,您永远不应该尝试访问它,即使是删除事件处理程序也是如此。
private void StopTimer()
{
if (TmrCount != null)
{
TmrCount.Stop();
TmrCount.Tick -= TmrCount_Tick;
TmrCount.Dispose();
TmrCount = null;
}
}
您好我在尝试制作一个简单的事件调度程序时遇到了一些问题。 在下面的代码中,我的目的是增加或减少时间(当使用鼠标事件按下按钮时)。
但是试了很多次都搞不明白为什么能增加;但不是减少时间。
// Timer
private Timer TmrCount;
private int HH {get;set;}
private int MM { get; set; }
private Button CurrBtn = new Button();
#region <Mouse Events>
private void OnMouseDown(object sender, EventArgs e)
{
CurrBtn = (Button)sender;
StartTimer();
}
private void OnMouseUp(object sender, MouseEventArgs e)
{
StopTimer();
}
#endregion
#region <Timer>
private void StartTimer()
{
if (TmrCount == null)
{
TmrCount = new Timer();
TmrCount.Interval = 210;
TmrCount.Tick += TmrCount_Tick;
TmrCount.Start();
}
}
private void TmrCount_Tick(object sender, EventArgs e)
{
Set_Time();
}
private void StopTimer()
{
if (TmrCount != null)
{
TmrCount.Stop();
TmrCount.Dispose();
TmrCount.Tick -= TmrCount_Tick;
TmrCount = null;
}
}
private void Set_Time()
{
switch (CurrBtn.Text)
{
case "+":
// Condition Check (Increase HH))
//if (HH == 23) { HH = default(int); }
//Increase HH
//if (HH < 23) { HH += 1; }
while (HH < 23) { HH++; break; }
break;
case "-":
// Condition Check (Decrease HH)
//if (HH == default(int)) { HH = 23; }
//if (HH == 0) { HH = 23; }
// Decrease HH
while (HH > 23) { HH-=1; break; }
break;
}
// Set Hour Text into Label
lbl_HH.Text = Convert.ToString(HH);
}
#endregion
谁能给我指出正确的方向? 提前致谢
假设您的 HH 变量以值 1 开头,那么您应该以这种方式更改您的 SetTime
int HH = 1;
private void Set_Time()
{
switch (CurrBtn.Text)
{
case "+":
// Condition Check (Increase HH))
// Within a limit of 23
while (HH < 23) { HH++; break; }
break;
case "-":
// Condition Check (Decrease HH)
// Decrease HH but don't allow it to be less than 0
while (HH >= 0) { HH-=1; break; }
break;
}
// Set Hour Text into Label
lbl_HH.Text = Convert.ToString(HH);
}
顺便说一句,在处置一个对象后,您永远不应该尝试访问它,即使是删除事件处理程序也是如此。
private void StopTimer()
{
if (TmrCount != null)
{
TmrCount.Stop();
TmrCount.Tick -= TmrCount_Tick;
TmrCount.Dispose();
TmrCount = null;
}
}