问题描述
我正在尝试编写一个通过一个项目列表的方法,每个方法将它添加到表单中,然后等待用户输入一些数据并点击一个按钮。然而,我不知道我应该/可以用来创建这个。
I am trying to write a method that goes through a list of items - for each one adds it to a form, then waits for the user to input some data and click a button. However I have no idea of what I should/could be using to create this.
foreach (string s in List)
{
txtName.Text = s;
//wait for button click...
// When the user is ready, they click the button to continue the loop.
}
到目前为止,我发现的是$ code> EventWaitHandle class,这似乎只适用于线程。
So far all I have found is the EventWaitHandle
class, which seems to only apply for threads.
我该如何实现?
推荐答案
如果您还使用异步/等待,那么使用信令的方式可以使用信号 - 您可以等待被点击的按钮完成的任务例如 - 但是一般来说,您应该以更加事件驱动的方式考虑用户界面。
There are ways of doing this using signalling if you're also using async/await - you could await a task which is completed by the button being clicked for example - but in general you should think about user interfaces in a more event-driven way.
而不是让您的 foreach
循环这里,将索引保存到正在显示项目的集合中,并在每次单击该按钮时提前执行。 (当然,请记住检查是否有 更多的项目。)
Instead of having your foreach
loop here, keep an index into the collection for which item is being displayed, and advance it each time the button is clicked. (Remember to check whether or not there are more items to display, of course.)
这篇关于强制循环等待事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!