C#中如何暂停用户控制?

发布于 2024-10-05 10:54:45 字数 2981 浏览 2 评论 0原文

我在阅读 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;
        }

其余的按钮处理程序被省略以使帖子更短。另外,我还没有发布摩尔类,因为我认为该表格是我的暂停语句应该去的地方。当然我可能是错的。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘿咻 2024-10-12 10:54:45

Windows 应用程序并不是为了暂停等待用户输入而设计的 - 这是一个控制台应用程序范例。

处理此问题的更好方法是禁用(或隐藏)其他控件,直到设置难度为止。类似于:最初仅显示难度 NumericUpDown 和开始按钮。按下开始按钮后,启用/显示其余控件并开始。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文