如何使循环等待直到单击按钮
How to make loop wait until button is clicked
我正在开发一个数学游戏,它会逐一给出 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;
之后添加等待。尚未添加阅读文本框,因为如果没有按钮它就无法使用,所以它不包括在内。 "between" 还没有完成,所以这就是为什么我在它之前有 if with int of it
如果将它放在按钮单击事件中,它将起作用并在您单击按钮时触发。
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;
}
如果你总共想问 10 个问题,一次一个,你不需要使用循环并在其中等待。您可以简单地使用按钮单击事件来检查答案并更新问题标签。
将 Between
和 rnd
移动为 class 成员,这样您就可以通过多种方式访问它们。除了这些,创建两个整数来存储当前的正确答案,以及已经问了多少问题。
在我的回答中,我使用了这些名称:
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?
}
}
}
我正在开发一个数学游戏,它会逐一给出 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;
之后添加等待。尚未添加阅读文本框,因为如果没有按钮它就无法使用,所以它不包括在内。 "between" 还没有完成,所以这就是为什么我在它之前有 if with int of it
如果将它放在按钮单击事件中,它将起作用并在您单击按钮时触发。
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;
}
如果你总共想问 10 个问题,一次一个,你不需要使用循环并在其中等待。您可以简单地使用按钮单击事件来检查答案并更新问题标签。
将 Between
和 rnd
移动为 class 成员,这样您就可以通过多种方式访问它们。除了这些,创建两个整数来存储当前的正确答案,以及已经问了多少问题。
在我的回答中,我使用了这些名称:
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?
}
}
}