-1

Head First C#を読みながら、この小さなプログラムを作成しました。ユーザーが難易度の数値アップダウン コントロールを設定できるように、プログラムを一時停止しようとしています。これがコードです...

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;
        }

投稿を短くするために、残りのボタン ハンドラーは省略されました。また、一時停止ステートメントをフォームに配置する必要があるため、モル クラスは投稿していません。もちろん、私は間違っているかもしれません。

4

1 に答える 1

1

Windows アプリケーションは、ユーザー入力を待って一時停止するようには設計されていません。これは、コンソール アプリケーションのパラダイムです。

これを処理するより良い方法は、難易度が設定されるまで他のコントロールを無効にする (または非表示にする) ことです。のようなもの: 最初は難易度 NumericUpDown と開始ボタンのみを表示します。開始ボタンが押されたら、残りのコントロールを有効/表示して開始します。

于 2010-11-30T22:18:14.257 に答える