vb.net 重置文本框的背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14970525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reset background color of textbox
提问by jamesdlivesinatree
I'm using VS2012 with VB.NET on a winforms application. I set the BackColor property of some textboxes programmatically during my code depending on form validation. This works fine, the problem is that I'd like to "reset" the BackColor property of the textbox, so that the textbox performs as if it were in the same state before I set the BackColor. So it would do the following:
我在 winforms 应用程序上使用 VS2012 和 VB.NET。根据表单验证,我在代码期间以编程方式设置了一些文本框的 BackColor 属性。这很好用,问题是我想“重置”文本框的 BackColor 属性,以便在设置 BackColor 之前,文本框的表现就好像它处于相同的状态。所以它会做以下事情:
Return to the default color of white immediately after "reset"
“重置”后立即返回默认的白色颜色
Change to that "light gray" color when the textbox.enabled = false
当 textbox.enabled = false 时更改为“浅灰色”颜色
The reason why I cannot simply set the BackColor to Color.White, is that this affects the textbox when textbox.enabled = false. The textbox does not return that "light gray" color after setting the backcolor and disabling the textbox. I need it to return to that color, and I'd rather not have to set the textbox's color everytime I enable or disable the textbox. Thanks!
我不能简单地将 BackColor 设置为 Color.White 的原因是,当 textbox.enabled = false 时,这会影响文本框。设置背景色并禁用文本框后,文本框不会返回“浅灰色”颜色。我需要它返回到那个颜色,而且我不想每次启用或禁用文本框时都必须设置文本框的颜色。谢谢!
回答by SysDragon
Simply:
简单地:
TextBox1.BackColor = SystemColors.Window
回答by Hans Passant
You reset the color by re-assigning the original value of BackColor. Or by assigning the default value, it isn't white:
您可以通过重新分配 BackColor 的原始值来重置颜色。或者通过分配默认值,它不是白色的:
textBox1.BackColor = Color.FromKnownColor(KnownColor.Window);

