如何使循环等待直到单击按钮

时间:2018-04-13 14:33:32

标签: c# winforms wait

我正在开发一个数学游戏,逐一提供10个不同的添加问题。这些问题显示在标签上,您可以通过写入文本框并单击提交来回答。我一直坚持回答部分,尤其是循环等待,直到我按下按钮。

从查找起,我找到了一种方法将其作为一个新事件,但我还没有得到如何使我的循环等待该事件继续

我的代码看起来像这样

int Between = 1;
        Random rnd = new Random();
        for (int i = 0; i < 10; i++)
        {
            if (Between == 1)
            {
                int num1 = rnd.Next(1, 11); // 1-10
                int num2 = rnd.Next(1, 11); // 1-10
                string number1 = num1.ToString();
                string number2 = num2.ToString();
                kusimus.Text = number1 + " + " + number2;
            }

我需要在kusimus.Text = number1 + " + " + number2;之后添加等待。阅读文本框尚未添加,因为没有按钮就无法使用,因此不包括在内。 &#34;之间&#34;我没有完成eiter,这就是为什么我在它之前使用int的原因

2 个答案:

答案 0 :(得分:1)

如果你想一共询问10个问题,你不需要使用循环并在其中等待。您只需使用按钮单击事件来检查答案并更新问题标签。

Betweenrnd移动为类成员,以便您可以使用多种方法访问它们。除此之外,创建两个整数来存储当前正确答案,以及已询问了多少个问题。

对于我的回答,我使用了这些名字:

private int Between = 1;
private Random rnd = new Random();
private int questionsAsked = 0;
private int currentAnswer = 0;

更新表单构造函数中第一个问题的标签,如下所示。

public Form1()
{
    InitializeComponent();

    // Get two random numbers
    int num1 = rnd.Next(1, 11); // 1-10
    int num2 = rnd.Next(1, 11); // 1-10

    // Save the answer.
    currentAnswer = num1 + num2;

    // Update the label.
    kusimus.Text = String.Format("{0} + {1}", num1, num2);

    // Keep track of how many questions have been asked.
    questionsAsked++;
}

然后在点击事件中做很多相同的事情,包括回答检查。

private void button1_Click(object sender, EventArgs e)
{
    // We've already asked ten questions, don't do anything else.
    if (questionsAsked > 10) return;

    // If the user entered a valid integer into the text box
    int answer;
    if (int.TryParse(txtBoxAnswer.Text, out answer))
    {
        // Implement Between if still needed.
        if (Between == 1)
        {
            if (answer == currentAnswer)
            {
                // the answer is correct.
            }
            else
            {
                // the answer is incorrect
            }

            int num1 = rnd.Next(1, 11); // 1-10
            int num2 = rnd.Next(1, 11); // 1-10

            currentAnswer = num1 + num2;

            kusimus.Text = String.Format("{0} + {1}", num1, num2);
        }

        // We've asked another question.
        questionsAsked++;

        if (questionsAsked > 10)
        {
            // User has answered last question, do something?
        }
    }
}

答案 1 :(得分:0)

如果将它放在按钮单击事件中,它将在您单击按钮时起作用并触发。

private void Button_Click(object sender, RoutedEventArgs e)
{
    int between = 1;
    Random rnd = new Random();

    //This loop is pointless since there's only one number that can use it.
    //However; I've left it as it incase you're needing it for another reason.
    for (int i = 0; i < 10; i++)
    {
        if (between == 1)
        {
            int num1 = rnd.Next(1, 11); // 1-10
            int num2 = rnd.Next(1, 11); // 1-10
            string number1 = num1.ToString();
            string number2 = num2.ToString();
            kusimus.Text = number1 + " + " + number2;
        }
    }
}

现在,还有一些注意事项,仅供参考:

/* If the work is a loop, string manipulation, or anything that may require more than 100ms of work
* then I suggest doing it asynchronously. Hopefully this helps.
* If it's confusing or you need more comments to explain what's going on let me know.
* Don't worry about the work being done... I just tried to keep it as similar as I could to your question
* and still make it useful for the example. 
* Note: This is WPF so the Textblock works like this but it should be RichTextBox for WinForms and button will just be button.Enabled = true : false */
private async void Button_Click(object sender, RoutedEventArgs e)
{
    button1.IsEnabled = false;
    textblock1.Text = string.Empty;

    var between = 1;
    Random rnd = new Random();

    var randomText = await Task.Run(() =>
    {
        var stringBuilder = new StringBuilder();
        for (int i = 0; i < 1000; i++)
        {
            if (between == 1)
            {
                int num1 = rnd.Next(1, 11); // 1-10
                int num2 = rnd.Next(1, 11); // 1-10
                string number1 = num1.ToString();
                string number2 = num2.ToString();
                stringBuilder.AppendLine(number1 + " + " + number2);
            }
        }
        return stringBuilder.ToString();
    });

    textblock1.Text = randomText;
    button1.IsEnabled = true;
}