禁用按钮
我想防止用户在按钮已经执行且过程尚未完成时单击按钮两次。
我使用的是紧凑框架 3.5,当用户在已经执行的按钮或其他按钮上单击两次时,我会遇到问题。我想在程序执行时禁用所有按钮,并在进程完成时再次启用它们。
操作系统:Windows mobile 6.1
框架:.NET 3.5 CF
I want to prevent the user clicking two times on a button when it has been already executing and the process is not finished.
I am using compact framework 3.5 and I have problems when the user clicks two times on a button that is already executing or some other button. I want to disable all buttons when the program is executing and enable them again when the process is done.
OS: Windows mobile 6.1
Framework: .NET 3.5 CF
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试在 Click 处理程序的范围内添加 this.Enabled = false 第一件事(这是有问题的表单)。完成后请务必将其设置回 true。如果这一切都在处理程序的范围内,您可能需要 Application.DoEvents() 或 Update() 来显示可见的进度。进行任何扩展处理的首选方法可能是生成一个后台线程并使用 Invoke 和 BeginInvoke 从中更新您的 UI。
Try adding this.Enabled = false first thing (this being the form in question) in the scope of your Click handler. Be sure to set it back to true when done. You may need to Application.DoEvents() or Update() to display visible progress if this all in the scope of the handler. Probably the preferred way to do any extended processing though would be to spawn a background thread and update your UI from it using Invoke and BeginInvoke.
我发现在构建 Windows Mobile 应用程序时需要经常执行此操作,因此创建了一个简单的实用程序类。
我需要做的就是拨打一条热线来锁定表格。
FormUtility.FormState(this.Controls, false);
您最终应该得到类似
EDIT 的结果:我认为 @tcarvin 的建议是您不需要在每个控件上调用刷新,而只需使控件,然后刷新容器,这将导致所有无效的控件立即重绘。我还没有测试过这个,但是对类似的东西做了一个小改变......
然后使用
I found that I needed to do this quite often when building a windows mobile application so made a simple utility class.
All I need to do was then call one line to lock the form down.
FormUtility.FormState(this.Controls, false);
You should end up with something like
EDIT : I think what @tcarvin is suggesting is that you do not need to call refresh on every control but simply invalidate the controls and then refresh the container which will cause all the invalidated controls to redraw at once. I haven't tested this but a small change to something like...
Then use
对于名为
button1
的按钮,这是最简单的方法:This is the easiest way, for a button called
button1
: