这是Form1
我正在尝试使用来自form2的变量级别在form1中设置计时器的间隔
int time = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
time++;
计时器的间隔是根据位于form2中的变量“ level”的值来设置的
if (Form8.level.Equals("expert"))
{
timer1.Interval = 10000;
if (time == 10000)
{
timer1.Stop();
MessageBox.Show("time ended");
}
}
else
{
timer1.Equals(60000);
if (time==60000)
{
timer1.Stop();
MessageBox.Show("time ended");
}
}
}
这是Form2
这是我要在form1中使用的变量
public Static String level = "";
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form1 f1 = new Form1();
f1.Show();
}
好的,我不认为您的代码确实是错误的,只是有些问题可能会给人以您的代码无法正常工作的印象。
首先,我建议您考虑更适当地命名您的班级。我个人倾向于以表单的功能命名。因此,您可以考虑将其重命名Form8
为DifficultyForm
。这样,更容易理解您的代码。另外,类似的名称也可以elapsedLevelTime
说明计时器的用途。如果你的代码别人看起来,这个人不会真正知道使用的timer1
。
现在,让我们快速回顾一下代码应该做什么:用户似乎选择了某种难度。然后,显示另一个窗口。在显示的窗口中,计时器应递增,直到达到特定时间为止。然后将显示文本“时间结束”。
如您所述,您在Designer中创建了很多东西。因此,我假设所有事件均已正确附加。得出结论,罪魁祸首位于您的中timer1_elapsed
,该代码实际上是对经过时间的计数:
private void timer1_Tick(object sender, EventArgs e)
{
time++;
if (Form8.level.Equals("expert"))
{
timer1.Interval = 10000;
if (time == 10000)
{
timer1.Stop();
MessageBox.Show("time ended");
}
}
else
{
timer1.Equals(60000);
if (time==60000)
{
timer1.Stop();
MessageBox.Show("time ended");
}
}
}
在调试算法时,Rubber duck调试(尽管有个有趣的名字)是一种有价值的方法。因此,让我们逐步完成代码。
表单加载后计时器启动。您并未真正指定计时器设置,但是默认计时器的间隔为100毫秒。因此,该tick
方法每秒提高约10次。
当计时器计时时,time
将增加一。因此1秒time
后将是10。
现在,您正在检查所选级别是否为专家。因此,我们必须在这里进行选择:
一个。级别是专家。在这种情况下,请将时间间隔设置为10000
。这将导致tick
事件每10秒引发一次。然后,您检查time
-variable是否为10000
。在这种情况下,您结束计时器并显示“时间已结束”。这是两个问题:
if (time == 10000)
而不是if (time >= 10000)
。这样,如果time
没有检查就更新了变量,则计时器将永远无法完成10000
条件将后达到10000 * 10s
秒,这是约27小时。b。该级别不是专家。在这里,您的代码也有一些问题:
timer1.Equals(60000);
是一个无用的声明。该Equals
方法将基本上检查是否timer1 == 60000;
。if
。见上:a160000 * 0.1s
大约100分钟后达到。以下是一些改进代码的建议:
在滴答事件之外设置计时器间隔。看起来只需要设置一次。
检查的用法 timer1.Equals(60000)
检查您是否真的想要这些长间隔
确保您了解价值所在time
以及价值增加的频率
我试图用可能的解决方案在一个小的WindowsForm项目中重新创建您的问题:
public partial class Form1 : Form
{
private Timer secondMeasureTimer;
private int elapsedTime; //in seconds
private string level = "expert";
public Form1()
{
InitializeComponent();
secondMeasureTimer = new Timer();
secondMeasureTimer.Tick += secondMeasureTimer_Tick;
secondMeasureTimer.Interval = 1000; // 1 second resolution
}
void secondMeasureTimer_Tick(object sender, EventArgs e)
{
#if DEBUG
System.Diagnostics.Debugger.Log(0, "secondMeasure", "secondMeaserTimer ticked at " + DateTime.Now + Environment.NewLine); // Output every time the event is raised for debugging purposes
#endif
elapsedTime++; // increas time every second
if (level == "expert" && elapsedTime >= 10)
{
textBox1.Text = "10 seconds elapsed";
secondMeasureTimer.Stop();
}
else if (elapsedTime >= 60)
{
textBox1.Text = "60 seconds elapse";
secondMeasureTimer.Stop();
}
}
private void timerButton_Click(object sender, EventArgs e)
{
if (secondMeasureTimer.Enabled)
{
secondMeasureTimer.Stop();
}
else
{
elapsedTime = 0;
secondMeasureTimer.Start();
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句