如何使winforms程序等待(例如,按“enter”)? [英] How to make a winforms program wait (eg. For pressing “enter”)?

查看:68
本文介绍了如何使winforms程序等待(例如,按“enter”)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有WinForm应用程序。我有功能。在这个功能中有一个时刻,当我希望它停止前进并等待用户按输入键或指定按钮



我期待也许一些那种无限循环,等待按下特定的键盘按键?



我尝试了什么:



I got WinForm Application. I got function in it. And there is a moment in this function, when I want it to stop going forward and wait for user pressing "enter key" or specified button

I look forward maybe some kind of infnity loop, waiting for specific keyboard key pressed?

What I have tried:

Consol.ReadKey();
//ihad this error
//Cannot read keys when either application does not have a console or when console //input has been redirected from a file. Try Console.Read.
MessageBox.Show("button pressed");

推荐答案

差不多,你不能 - 或者至少你肯定不应该这样。



问题在于,与控制台应用程序不同,WinForms是消息驱动的,因此如果您的代码正在等待用户按键,则应用程序的其余部分无法执行任何操作 - 包括更新显示或响应系统事件。这很糟糕 - 非常糟糕。



您应该考虑的是将功能分解为块,并使用标准按钮按下事件处理程序以进展到下一阶段 。或者更好的是,重新设计您的应用程序,使其以正常方式工作,响应用户事件,而不是试图驱使用户做某事。强制用户响应是控制台中发生的事情,而不是图形UI应用程序。
Pretty much, you can't - or at least you certainly shouldn't.

The problem is that unlike a console app, WinForms are message driven, so if your code is sitting waiting for the user to press a key, the rest of your application can't do anything - including updating the display or responding to system events. That's bad - very bad.

What you should consider instead is breaking your function into chunks, and using standard Button presses event handlers to "progress" to the "next stage". Or better, redesign your app so it works in "normal" ways, responding to user events instead of trying to drive the user to do something. Forcing the user to respond is what happens in consoles, not graphic UI applications.


这篇关于如何使winforms程序等待(例如,按“enter”)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆