简体   繁体   English

用户单击消息对话框“确定”按钮时重新启动代码

[英]Restarting code when user clicks message dialogue “ok” button

I am trying to spice up my code for a simple number guessing game I am making. 我正在尝试为我正在制作的一个简单的猜数游戏调整我的代码。 Once the user guesses the proper number a message dialogue box pops up telling him he is a winner. 一旦用户猜出正确的号码,就弹出一个消息对话框,告诉他他是胜利者。 That message dialogue box has one button named "ok" when I click it I am taken back to the form. 当我点击它时,该消息对话框有一个名为“ok”的按钮我被带回到表单中。

Instead I would like it to restart the code so that a new random number is generated. 相反,我希望它重新启动代码,以便生成一个新的随机数。 Is this possible or do I need to manually revert the code back to its default state within the winner code area? 这是可能的还是我需要手动将代码恢复到赢家代码区域内的默认状态?

Here is my code now: 这是我现在的代码:

    private void btnEval_Click (object sender, EventArgs e)
    {
        // increment the counter to be displayed later
        howManyClicks++;



        // main decision conditions, ensures something is entered
        if (txtNum.Text != "")
        {
            // user input number
            double num = double.Parse (txtNum.Text);

            if (randomNumber > num)
            {
                // if too low
                this.BackColor = Color.Yellow;
                lblMain.Text = "TOO LOW!";
                lblReq.Text = "please try again";
                txtNum.Clear ();
                txtNum.Focus ();

            }
            else if (randomNumber < num)
            {
                // if too high
                this.BackColor = Color.Red;
                lblMain.Text = "TOO HIGH!";
                lblReq.Text = "please try again";
                txtNum.Clear ();
                txtNum.Focus ();
            }
            else
            {
                // correct
                this.BackColor = Color.Green;
                lblMain.Text = "CORRECT!";
                lblReq.Text = "well done";
                MessageBox.Show ("You are right!! It took you " + howManyClicks + " guesses", "You are a WINNER!!",
                    MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                txtNum.Clear ();
                txtNum.Focus ();
            }
        }
        else
        {
            MessageBox.Show ("You must enter a vaild number! Please try again.", "ERROR",
                MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            txtNum.Clear ();
            txtNum.Focus ();
        }

Apparently, your "game state" ( howManyClicks , randomNumber , ...) is stored in instance variables of the form. 显然,您的“游戏状态”( howManyClicksrandomNumber ,...)存储在表单的实例变量中。 Thus, you have the following options: 因此,您有以下选择:


Extract the game state into its own class and keep only a reference to an instance of this class in your form: 将游戏状态提取到自己的类中,并在表单中仅保留对此类实例的引用:

GameState state;

When you start or restart the game, just assign a new GameState() to state and reset the user interface elements of the form. 当您启动或重新启动游戏时,只需指定一个new GameState()state并重置表单的用户界面元素。


Alternatively, you can close and reopen the form. 或者,您可以关闭并重新打开表单。 Your main loop could look something like this: 你的主循环看起来像这样:

while (true) {
    var form = new GameForm();
    var result = form.ShowDialog(); // waits until the form as been closed

    if (result == DialogResult.Cancel) {
        break;  // The user wants to stop playing
    }
}

In your game form, when the OK button is clicked, you set Me.DialogResult to DialogResult.OK and then close the form. 在游戏表单中,单击“ OK按钮时,将Me.DialogResult设置为DialogResult.OK ,然后关闭表单。 The outer loop will automatically reopen a new, empty game form. 外循环将自动重新打开一个新的空游戏表单。

Simply make function for this code. 只需为此代码生成函数即可。

Select all the code and then right click on it, select second option: Refactor 选择所有代码,然后右键单击它,选择第二个选项: Refactor

Select suboption: Extract a method . 选择子选项: Extract a method

You can give name for this public method. 您可以为此公共方法指定名称。

Then you can call this method from anywhere,( as you have mentioned restart the code). 然后你可以从任何地方调用这个方法(正如你提到的那样重启代码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户单击以关闭消息窗口时发生的事件 - Event when user clicks to close message window 当用户单击 ASP.NET MVC controller 中的“添加到购物车”按钮时显示消息 - Display message when user clicks on "Add To Cart" button in ASP.NET MVC controller 用户单击按钮时重定向到登录页面 - Redirecting to login page when user clicks a button 用户单击单选按钮时触发事件 - Firing an event when a user clicks a radio button 当用户单击按钮时,我编写了用于隐藏当前表单并显示下一个表单的代码 - When the user clicks a button,then I have write code for hiding the current form and showing the next form 需要在用户单击按钮时同时调用 JavaScript 函数和代码隐藏函数 - Need to call a JavaScript function and a code behind function at the same time when a user clicks a button 当用户在作为 ShowDialog 启动的 window 之外单击时显示消息 - Show a message when user clicks outside a window thats launched as ShowDialog 用户单击批准后如何显示确认消息? - how can i put a confirmation message when the user clicks approved? 用户单击复选框时如何显示确认消息 - How to display confirmation message when a user clicks a checkbox 当用户单击“取消”按钮并退出时,放弃在Gridcontrol中所做的更改 - Discard changes made in a Gridcontrol when a user clicks the “Cancel” button and exit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM