开发者

How to pause for user control in C#?

I made this little program while reading Head First C#. I'm trying to get the program to pause for the user to set the numericUpDown control for the level of difficulty. Here's the code...

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 Le开发者_Python百科vel");
            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;
        }

The rest of the button handlers were omitted to make post shorter. Also I haven't posted the mole class because I think the form is where my pause statement should go. Of course I could be wrong.


Windows applications aren't designed to pause waiting for user input - that's a console application paradigm.

A better way to handle this is to disable (or hide) other controls until the difficulty has been set. Something like: initially show only difficulty NumericUpDown and a start button. Once the start button has been pressed enable/show the remaining controls and begin.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜