我正在进行记忆游戏,我需要设置一个计时器来重置de images,如果它们不匹配但是它不能将图片重新设置为隐藏:
//set images
if (clickedLabel != null)
{
var Index = Convert.ToInt32(clickedLabel.Tag);
clickedLabel.Image = icons[Index];
//Check first clicked
if (firstClicked == null)
{
firstClicked = clickedLabel;
return;
}
secondClicked = clickedLabel;
timer1.Start();
}
方法timer1_Tick
:
//Timer
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
firstClicked = null;
secondClicked = null;
}
答案 0 :(得分:0)
这可能有效:
class YourClass
{
System.Timers.Timer aTimer;
void YourMethod()
{
//code
if (clickedLabel != null)
{
var Index = Convert.ToInt32(clickedLabel.Tag);
clickedLabel.Image = icons[Index];
//Check first clicked
if (firstClicked == null)
{
firstClicked = clickedLabel;
return;
}
secondClicked = clickedLabel;
SetTimer();
}
private static void SetTimer ()
{
//System.Timers.Timer aTimer; at the beginning of your class
aTimer = new System.Timers.Timer (2000); //the time you want in milliseconds
aTimer.Elapsed += OnTimedEvent;
aTimer.AutoReset = false;
aTimer.Enabled = true;
}
}
AutoReset设置为false将使Elapsed事件仅触发一次。然后,你在那个事件中做你想做的事。
private static void OnTimedEvent (Object source, ElapsedEventArgs e)
{
firstClicked = null;
secondClicked = null;
}
当您不再需要时,您可能希望在aTimer上使用Stop和Dispose方法。
在发布问题时,您也应该更加具体,以确保获得所需的帮助:)