我在阅读Head First C#时制作了这个小程序。我试图让程序暂停,让用户为难度级别设置 numericUpDown 控件。这是代码...
namespace WACK
{
public partial class Form1 : Form
{
public int level;
Mole mole;
Random random = new Random();
public Form1()
{
InitializeComponent();
mole = new Mole(random, new Mole.PopUp(MoleCallBack));
MessageBox.Show("Select a Level");
if (level == 1)
{
timer1.Interval = random.Next(500, 500);
}
if (level == 2)
{
timer1.Interval = random.Next(400, 400);
}
if (level == 3)
{
timer1.Interval = random.Next(300, 300);
}
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
ToggleMole();
}
private void ToggleMole()
{
if (mole.Hidden == true)
mole.Show();
else
mole.HideAgain();
if (level == 1)
{
timer1.Interval = random.Next(500, 500);
}
if (level == 2)
{
timer1.Interval = random.Next(400, 400);
}
if (level == 3)
{
timer1.Interval = random.Next(300, 300);
}
timer1.Start();
}
private void MoleCallBack(int moleNumber, bool show)
{
if (moleNumber < 0)
{
timer1.Stop();
return;
}
Button button;
switch (moleNumber)
{
case 0: button = button1; break;
case 1: button = button2; break;
case 2: button = button3; break;
case 3: button = button4; break;
case 4: button = button5; break;
case 5: button = button6; break;
case 6: button = button7; break;
case 7: button = button8; break;
default: button = button9; break;
}
if (show == true)
{
button.Text = "Hit Me!";
button.BackColor = Color.Red;
}
else
{
button.Text = "";
button.BackColor = SystemColors.Control;
}
timer1.Interval = random.Next(500, 1000);
timer1.Start();
}
private void button1_Click(object sender, EventArgs e)
{
mole.Smacked(0);
}
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
level = (int)numericUpDown1.Value;
}
其余的按钮处理程序被省略以使帖子更短。另外我还没有发布鼹鼠类,因为我认为表单是我的暂停语句应该去的地方。当然我可能是错的。