C# 如何循环使用方法x多次?
我有一个这样的方法这个项目C# 如何循环使用方法x多次?,c#,winforms,loops,C#,Winforms,Loops,我有一个这样的方法这个项目 private void LoopThis() { MessageBox.Show("Hello World!"); } 我有一个按钮调用这个方法,还有一个文本框,在这里我输入一个int,比如说输入10 我希望该方法执行10次 我使用什么样的循环来执行此操作?私有无效按钮\u单击(对象发送方,路由目标) private void Button_Click(object sender, RoutedEventArgs e) { int times;
private void LoopThis()
{
MessageBox.Show("Hello World!");
}
我有一个按钮调用这个方法,还有一个文本框,在这里我输入一个int,比如说输入10
我希望该方法执行10次
我使用什么样的循环来执行此操作?私有无效按钮\u单击(对象发送方,路由目标)
private void Button_Click(object sender, RoutedEventArgs e)
{
int times;
if (Int32.TryParse(TextBox.Text, out times))
{
// It could parse the input text (so we deduce it was an integer)
// and not a string.
for (int i = 0; i < times; i++)
{
LoopThis();
}
}
else
{
// Throw exception or show a message to the user
}
}
{
整数倍;
if(Int32.TryParse(TextBox.Text,超时))
{
//它可以解析输入文本(因此我们推断它是一个整数)
//而不是一根绳子。
for(int i=0;i
你真的应该读一本书或一本非常基础的教程,在StackOverflow中问一些琐碎的问题是浪费时间。你的问题是什么?你知道for…loop吗?或者,您正在询问如何检索文本框的内容并将其转换为整数?如果您键入“ABC”而不是整数怎么办?然后我只会抛出一个带有messagebox的异常。@Steve我已更新了代码以使用TryParse
,以防转换失败。