System.Windows.Forms.Panel.Enabled = 假颜色过载?
我在 Windows 应用程序中有一个 System.Windows.Forms.Panel 控件,该控件在面板中有许多标签和文本框。 当您调用 Panel.Enabled = false 命令时,所有文本框和标签都会变为灰色并变得不可编辑,这是所需的效果。 当面板被禁用时,有没有办法让浅灰色超载?
使用的解决方案:
由于禁用时无法覆盖禁用的颜色,因此以下内容仅用于禁用面板上的文本框和标签,而不是空白的Panel.Enabled = false;。
//Loop through the Member info Panel and find all the group boxes
foreach (Control cItem in Panel.Controls)
{
//A group box is found
if (cItem is GroupBox)
{
//Loop through all the group box components
foreach (Control cSubItem in cItem.Controls)
{
//If its a label of text box disable it
if (cSubItem is TextBox || cSubItem is Label)
{
cSubItem.Enabled = false;
}
}
}
}
i have a System.Windows.Forms.Panel control in a windows application that has many labels and textboxes within the panel. When you call the Panel.Enabled = false command all of the textboxes and labels go gray and become un-editable which is the desired effect. Is there a way to overload the light gray colors when the panel is disabled?
Solution used:
Since the disabled color cant be overiden when disabled the following was used to disable only text boxes and labels on the Panel rather than the blanked Panel.Enabled = false;.
//Loop through the Member info Panel and find all the group boxes
foreach (Control cItem in Panel.Controls)
{
//A group box is found
if (cItem is GroupBox)
{
//Loop through all the group box components
foreach (Control cSubItem in cItem.Controls)
{
//If its a label of text box disable it
if (cSubItem is TextBox || cSubItem is Label)
{
cSubItem.Enabled = false;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
无法全面覆盖每个标签和文本框在禁用时的着色方式。 但是,您可以在禁用包含面板后覆盖每个单独的控件并将它们设置为适当的颜色。
当禁用时,TextBox 类(更具体地说是 TextBoxBase 类)将更喜欢使用显式颜色而不是默认的灰色进行绘制。
我不久前写了一篇关于如何实现这种效果的博客文章: http://blogs.msdn.com/jaredpar/archive/2007/02/12/readonly-textbox-that-doesn-t-look-funny.aspx
There is no way to do a blanket override of how each Label and TextBox are Colored when they are disabled. But you can override each individual control after the containing panel is disabled and set them to the appropriate color.
The TextBox class (and more specifically the TextBoxBase class) will prefer an explicit color over the default Gray color for painting when disabled.
I wrote a blog post awhile back on how to achieve this effect: http://blogs.msdn.com/jaredpar/archive/2007/02/12/readonly-textbox-that-doesn-t-look-funny.aspx
作为备选:
如果您希望控件具有不同的启用/禁用颜色,您可以迭代在面板中注册的控件,并为它们的 EnabledChanged 事件分配一个自定义函数来更改它们的颜色(基于我假设的类型)。 您可能想用它来过滤您修改的控件类型,但我相信您可以使用它来实现您的目标。
更新:
尝试这个:
public void deselected(对象发送者,EventArgs e)
{
foreach(this.Controls 中的 Control c)
{
//TODO:检查控件类型并更改背景颜色
}
}
需要将其放置在 Form 类中,但随后您可以在事件中使用它。 希望有帮助。
As an alternative:
If the controls you wish to have a different enabled/disabled color you could iterate through the controls registered with the panel and assign their EnabledChanged event a custom function to change their color (based on type I assume). You'd probably want to filter the type of control you modify with this but you could use this to achieve your goal I believe.
Update:
Try this:
public void deselected(object sender, EventArgs e)
{
foreach (Control c in this.Controls)
{
//TODO:check type of control and change background color
}
}
That will need to be placed inside your Form class but then you can use it on a event. Hope that helps.