Winforms 如何在C#中暂停用户控件?

Winforms 如何在C#中暂停用户控件?,winforms,user-controls,pausing-execution,Winforms,User Controls,Pausing Execution,我在阅读的时候做了这个小程序。我试图让程序暂停,以便用户根据难度设置numericUpDown控件。这是密码 namespace WACK { public partial class Form1 : Form { public int level; Mole mole; Random random = new Random(); public Form1() { Initi

我在阅读的时候做了这个小程序。我试图让程序暂停,以便用户根据难度设置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;
        }
namespace-WACK
{
公共部分类Form1:Form
{
公共智力水平;
摩尔;
随机=新随机();
公共表格1()
{
初始化组件();
摩尔=新摩尔(随机,新摩尔.弹出(摩尔回调));
MessageBox.Show(“选择级别”);
如果(级别==1)
{
timer1.Interval=random.Next(500500);
}
如果(级别==2)
{
timer1.Interval=random.Next(400400);
}
如果(级别==3)
{
timer1.Interval=random.Next(300300);
}
timer1.Start();
}
私有无效计时器1_刻度(对象发送方,事件参数e)
{
timer1.Stop();
切换mole();
}
私有void ToggleMole()
{
if(mole.Hidden==真)
mole.Show();
其他的
鼹鼠;鼹鼠;
如果(级别==1)
{
timer1.Interval=random.Next(500500);
}
如果(级别==2)
{
timer1.Interval=random.Next(400400);
}
如果(级别==3)
{
timer1.Interval=random.Next(300300);
}
timer1.Start();
}
私人空间(整数摩尔数,布尔显示)
{
if(摩尔数<0)
{
timer1.Stop();
返回;
}
按钮;
开关(摩尔数)
{
案例0:按钮=按钮1;断开;
案例1:按钮=按钮2;断裂;
案例2:按钮=按钮3;断裂;
案例3:按钮=按钮4;断开;
案例4:按钮=按钮5;断开;
案例5:按钮=按钮6;断裂;
案例6:按钮=按钮7;断裂;
案例7:按钮=按钮8;断开;
默认值:button=button9;break;
}
if(show==true)
{
button.Text=“打我!”;
button.BackColor=Color.Red;
}
其他的
{
按钮。文本=”;
button.BackColor=SystemColors.Control;
}
timer1.Interval=random.Next(5001000);
timer1.Start();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
鼹鼠。打(0);
}
私有void numericUpDown1_值已更改(对象发送方,事件参数e)
{
级别=(int)numericUpDown1.Value;
}

其余的按钮处理程序被省略,以缩短post。此外,我还没有发布mole类,因为我认为表单是我的pause语句应该使用的地方。当然,我可能错了。

Windows应用程序的设计不是暂停等待用户输入-这是一种控制台应用程序范例


更好的处理方法是禁用(或隐藏)设置难度之前的其他控件。例如:最初仅显示难度数字向上向下和开始按钮。一旦按下开始按钮,启用/显示其余控件并开始。

我肯定应该返回并禁用控件这是一个好主意,但暂停是我一直坚持的。因此如果我加入start按钮调用切换摩尔(),你认为这样做会是一种方式吗?现在我想了想,我可以回答…试试看…如果它不起作用,想想别的。heheMaybe你可能错过了这个。不确定。但我发现我可以使用消息框接收用户输入。哇!一个暂停用户输入的win应用程序,我每天都在学习。是的,di展示一个模态对话框输入是另一种方式。酷,我实际上添加了一些有用的信息。我可能真的学到了一些东西。呵呵!再次感谢你帮助我这样一个没有头脑的新手!