按钮禁用和启用 [英] Button disable and enable

查看:40
本文介绍了按钮禁用和启用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 vb.net 的 Windows 应用程序,当点击GO"按钮时,一堆数据被加载到数据库中.因此,在我的应用程序中,一旦单击GO"按钮,我只想禁用它,并希望在上传完成后重新启用它.现在在我的 btnGo_Click() 特定方法中,我有:

I have a vb.net based windows application, where when "GO" button is clicked a bunch of data is loaded into DB. So in my application as soon as "GO" button is clicked I want to just disable it and would like to enable it back when the uploading has completed. Now in my specific method for btnGo_Click() I have:

btnGo.Enabled = False

作为第一行和

btnGo.Enabled = True

作为相同方法的最后一行.

as last line in the same method.

但我不明白为什么GO"虽然显示为已禁用,但在处理过程中仍允许单击.此外,如果我删除最后一行,它会被永久禁用并且不允许点击事件.

But I fail to understand why the "GO" though appears as being disabled still allows click when processing is going on. Also if I remove the last line, it gets disabled permanently and doesn't allow the click event.

请指出我做错了什么?

编辑(日期:2012 年 1 月 25 日):我按照同事的建议进行了更改,但我在这里遇到了一个新问题.我面临一个问题,即文本框会更新但并非总是如此.我已经在后台工作线程的_ProgressChanged"事件中更新了我的文本框.就我而言,如果上传了 10 条记录.然后有 10 行预期在 texbox 中的更新.但是文本框中只显示了几行.难道又是重绘问题?请建议...因为所有其他事情都是按照您的建议完成的

Edit (Dated: 25th Jan 2012): I made changes as suggested by our collegues, but I am facing a new issue here. I am facing an issue where the textbox gets updated but not always. I have updated my textbox in "_ProgressChanged" event of the background worker thread. In my case if there is 10 records uploaded. Then there are 10 lines of updates that are expected in the texbox. But only few lines are shown in the textbox. Is it the repaint issue again? Kindly suggest...Because all other things are done as per your suggestion

推荐答案

你没有做错任何事.问题是在事件处理程序方法中的代码完成执行之前,UI 不会更新.然后,按钮被禁用并立即快速启用.

You're not doing anything wrong. The problem is that the UI doesn't get updated until the code inside of your event handler method finishes executing. Then, the button is disabled and immediately enabled in rapid sequence.

这就解释了为什么如果您忘记在事件处理程序方法的末尾重新启用按钮控件,它仍然处于禁用状态——因为您在方法的第一行告诉它禁用按钮.

That explains why if you forget to reenable the button control at the end of the event handler method, it is still disabled—because you told it to disable the button in the first line of the method.

这是一个经典案例,说明为什么永远不要在事件处理程序方法内执行长时间运行的计算任务,因为它会阻止 UI 更新.计算实际上需要在单独的线程上进行.但是不要尝试手动创建线程,绝对不要尝试从单独的线程更新您的 UI.相反,使用 BackgroundWorker 组件 来处理所有这会自动为您服务.链接的 MSDN 文档有一个很好的示例说明如何使用它.

This is a classic case of why you should never perform long-running computational tasks inside of an event handler method, because it blocks the UI from being updated. The computation actually needs to happen on a separate thread. But don't try to create the thread manually, and definitely don't try to update your UI from a separate thread. Instead, use the BackgroundWorker component to handle all of this for you automatically. The linked MSDN documentation has a great sample on how to use it.

在启动 BackgroundWorker 之前禁用按钮,然后在它的 Completed 事件中重新启用它,表示数据库加载完成.

Disable the button before starting the BackgroundWorker, and then re-enable it in its Completed event, signaling the completion of your database load.

这篇关于按钮禁用和启用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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