简体   繁体   English

更改按钮的背景颜色

[英]Changing the backcolor of a button

I have a problem with changing the color of a button (in vb.net). 我有一个改变按钮颜色的问题(在vb.net中)。 I have searched the web and plenty of people are willing to tell me HOW TO change the color but my problem is - it doesn't. 我搜索过网络,很多人都愿意告诉我如何改变颜色,但我的问题是 - 它没有。 I say it doesn't, it does when it has finished the rest of the code in the Sub. 我说它没有,它完成了它完成Sub中的其余代码。 Can anyone furnish me with an explanation please as to why it doesn't happen when I ask? 任何人都可以向我提供一个解释,请问为什么在我问的时候不会发生这种情况? I'm using Visual Studio 2008 我正在使用Visual Studio 2008

After changing the colour call Application.DoEvents() 更改颜色调用后Application.DoEvents()

This gives the GUI a chance to update. 这为GUI提供了更新的机会。

Application.DoEvents will work, but it's the wrong solution for this. Application.DoEvents可以工作,但这是错误的解决方案。

The right solution has to do with the reason you don't want to wait for the end of the sub: you have some long running code in your method, and the method run in direct response to a UI event. 正确的解决方案与您不想等待子结束的原因有关:您的方法中有一些长时间运行的代码,并且该方法直接响应UI事件。 In this situation, the right solution is to use a BackgroundWorker component to execute the long-running code, so the method will complete right away and the button can change color when it's supposed to. 在这种情况下, 正确的解决方案是使用BackgroundWorker组件来执行长时间运行的代码,因此该方法将立即完成,按钮可以在应该的时候改变颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM