質問

読んでいる間、私はこの小さなプログラムを作りました 頭の最初の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;
        }

残りのボタンハンドラーは、ポストを短くするために省略されました。また、フォームが私の一時停止の声明が進むべき場所だと思うので、私はMoleクラスを投稿していません。もちろん、私は間違っている可能性があります。

役に立ちましたか?

解決

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

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top