C# 如何使循环等待按钮被单击
我正在做一个数学游戏,一个接一个地给出10个不同的加法问题。这些问题将显示在标签上,您可以通过在文本框中写入并单击提交来回答。我被困在回答部分,尤其是循环等待,直到我按下一个按钮 通过查找,我找到了一种方法将其作为新事件,但我不知道如何使循环等待该事件继续 我的代码如下所示C# 如何使循环等待按钮被单击,c#,winforms,wait,C#,Winforms,Wait,我正在做一个数学游戏,一个接一个地给出10个不同的加法问题。这些问题将显示在标签上,您可以通过在文本框中写入并单击提交来回答。我被困在回答部分,尤其是循环等待,直到我按下一个按钮 通过查找,我找到了一种方法将其作为新事件,但我不知道如何使循环等待该事件继续 我的代码如下所示 int Between = 1; Random rnd = new Random(); for (int i = 0; i < 10; i++) {
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;
}
int介于=1之间;
随机rnd=新随机();
对于(int i=0;i<10;i++)
{
如果(介于==1之间)
{
int num1=rnd.Next(1,11);//1-10
int num2=rnd.Next(1,11);//1-10
字符串number1=num1.ToString();
字符串number2=num2.ToString();
Text=number1+“+”+number2;
}
我需要在kusimus.Text=number1+“+”+number2;之后添加等待。读取文本框尚未添加,因为如果没有按钮,它将无法使用,因此不包括在内。“介于”没有完成eiter,所以这就是为什么我在它前面有if和int,如果你把它放在按钮点击事件中,它会工作,当你点击按钮时会触发
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;
}
}
}
private void按钮\u单击(对象发送者,路由目标)
{
int在=1之间;
随机rnd=新随机();
//这个循环毫无意义,因为只有一个数字可以使用它。
//不过,我把它留着,以防你出于其他原因需要它。
对于(int i=0;i<10;i++)
{
如果(介于==1之间)
{
int num1=rnd.Next(1,11);//1-10
int num2=rnd.Next(1,11);//1-10
字符串number1=num1.ToString();
字符串number2=num2.ToString();
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;
}
/*如果工作是循环、字符串操作或任何可能需要超过100毫秒工作的事情
*然后我建议异步执行。希望这能有所帮助。
*如果有什么让人困惑或者你需要更多的评论来解释发生了什么,请告诉我。
*不要担心正在做的工作……我只是尽量让它和你的问题保持一致
*并且仍然使其对示例有用。
*注意:这是WPF,所以Textblock的工作方式是这样的,但它应该是WinForms的RichTextBox,而button只是button.Enabled=true:false*/
专用异步无效按钮\u单击(对象发送方,路由目标)
{
按钮1.IsEnabled=错误;
textblock1.Text=string.Empty;
var介于=1之间;
随机rnd=新随机();
var randomText=等待任务。运行(()=>
{
var stringBuilder=新的stringBuilder();
对于(int i=0;i<1000;i++)
{
如果(介于==1之间)
{
int num1=rnd.Next(1,11);//1-10
int num2=rnd.Next(1,11);//1-10
字符串number1=num1.ToString();
字符串number2=num2.ToString();
stringBuilder.AppendLine(编号1+“+”+编号2);
}
}
返回stringBuilder.ToString();
});
textblock1.Text=randomText;
按钮1.IsEnabled=true;
}
如果你想问10个问题,一次一个,你不需要使用循环并在其中等待。你只需使用按钮单击事件检查答案并更新问题标签即可
在和rnd之间移动,成为类成员,这样您就可以通过多种方法访问它们。除此之外,创建两个整数来存储当前的正确答案,以及询问了多少问题
对于我的回答,我使用了以下名称:
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?
}
}
}
UI为您循环,您不必这样做。只需在按钮单击处理程序中添加答案检查代码。为什么需要循环?只需将按钮按下后要运行的代码放入单击事件处理程序。循环是在单击处理程序中同时执行10个问题,您可以更新标签以显示下一个问题,无需循环。数一数你问了多少次问题,然后停在神奇的数字前。