问题描述
在的WinForms .NET控件如果我们设置Enabled属性设置为false控件将显示为灰色。
In winforms .net controls if we set Enabled property to false the control will be grayed out.
在这种情况下,它会变得不可读许多颜色组合(因为我给的选项可以更改用户在运行时)形式的颜色。
In this case it will become unreadable for many color combinations (because i am giving options to change color of a form for user at run time).
我可以使用只读属性,但它仅适用于文本框控件没有像其他控件组合框,的DateTimePicker等。
I can use ReadOnly property but it is available only to TextBox controls not for other controls like ComboBox, DateTimePicker etc.
我只是想知道有没有什么选项可用,这样我就可以防止变灰当它是禁用的控件。
I just wanted to know is there any option available so that i can prevent controls from graying out when it is disabled.
推荐答案
这是在大多数的可用性研究的一个悲伤的时刻,眼看着主题乒乒乓乓的鼠标和键盘,而不是理解为什么它不工作。但你可以得到它,如果你真的想。诀窍是把一个图片框,显示在其先前启用状态的控件的位图控制的前面。他们永远不会搞清楚他们点击一个位图,而不是实际控制。
This is a sad moment in most any usability study, seeing the subject banging away at the mouse and keyboard and not understanding why it doesn't work. But you can get it if you really want to. The trick is to put a picture box in front of the control that shows a bitmap of the controls in their previously enabled state. They'll never figure out they are clicking on a bitmap instead of the actual controls.
最好用面板进行,因此您可以轻松地禁用控制为一组。添加一个新类到您的项目并粘贴如下所示的代码。编译。从工具箱的上方新的控制到窗体。并提出应禁用它里面的控制。其他一切都是自动的,只是Enabled属性设置为false,用户将不知道发生了什么:
Best done with a Panel so you can easily disable controls as a group. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. And put the controls inside it that should be disabled. Everything else is automatic, just set the Enabled property to false and the user won't know what happened:
using System;
using System.Drawing;
using System.Windows.Forms;
class FakeItPanel : Panel {
private PictureBox mFakeIt;
public new bool Enabled {
get { return base.Enabled; }
set {
if (value) {
if (mFakeIt != null) mFakeIt.Dispose();
mFakeIt = null;
}
else {
mFakeIt = new PictureBox();
mFakeIt.Size = this.Size;
mFakeIt.Location = this.Location;
var bmp = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(bmp, new Rectangle(Point.Empty, this.Size));
mFakeIt.Image = bmp;
this.Parent.Controls.Add(mFakeIt);
this.Parent.Controls.SetChildIndex(mFakeIt, 0);
}
base.Enabled = value;
}
}
protected override void Dispose(bool disposing) {
if (disposing && mFakeIt != null) mFakeIt.Dispose();
base.Dispose(disposing);
}
}
这篇关于从变灰,当它被禁用预防控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!