It is not a huge deal to create a read-only checkbox. I just did it for a
VB project. Below is an off-the-cuff (untested) C# version. The basic idea
is to cancel the effect of the CheckChanged event when ReadOnly = true. I
have also set the text associated with the checkbox to a slightly greyed
color so that it's still somewhat distinguishable from a non read-only
checkbox, yet still quite readable. You could add a DisabledForeColor
property if you wanted to make that more configurable.
I haven't bothered to put in design-time checks, so this version will refuse
to let you change the Checked property in the designer unless ReadOnly =
false.
Compile this as part of a separate library and add that library to your
designer controls toolbar.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DevLib.Win.Controls {
class ReadOnlyCheckBox: CheckBox {
protected Color savedForeColor;
protected bool readOnly = false;
protected bool checkChangeFlag = false;
protected bool saveForeColorChangeFlag = true;
public ReadOnlyCheckBox() : base() {
this.savedForeColor = this.ForeColor;
}
public override Color ForeColor {
get {return base.ForeColor;}
set {
base.ForeColor = value;
if (this.saveForeColorChangeFlag == true) {
this.savedForeColor = value;
}
}
}
public virtual bool ReadOnly {
get {return this.readOnly;}
set {
this.readOnly = value;
this.saveForeColorChangeFlag = false;
if (value == true) {
if (this.savedForeColor == Color.DarkSlateGray) {
this.ForeColor = Color.DarkGray;
} else {
this.ForeColor = Color.DarkSlateGray;
}
} else {
this.ForeColor = this.savedForeColor;
}
}
}
private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
{
if (this.readOnly == true) {
// Undo the change, making the control effectively read-only.
this.checkChangeFlag = true;
this.Checked = !this.Checked;
this.checkChangeFlag = false;
}
}
}
}
"James" <Ka***********@community.nospam> wrote in message
news:AA**********************************@microsof t.com...
I agree Steve, I have just spent an hour trying to make a checkbox both
"display only" only to look in the discussions and find that it seems that
it
will be yet another simple thing that is difficult to do.
Before anyone replys with "disabled controls should be greyed so the user
doesn't get confused about why they can't change them"... please give some
thought to users who don't want to go blind trying to see the status of
checkboxes etc. on a web form that is solely for enquiry. As my users are
not
morons, they can deal with "display only" controls being readable and in
any
colour they choose.
Please someone provide a usefull response to this.
Cheers,
Jamie/James
"Steve Le Monnier" wrote:
Is there any quick way of fixing combo boxes or radio buttons so that on
an
enquiry screen its possible to read their contents. As they don't have
read-only properties the only method available is to use the enabled
property, but this given an almost unreadable GUI.
In the VB days we used to dock troublesome controls onto frames and then
lock the frames but this is not an option anymore. Does anybody have an
alternative tip.
Its these kind of silly things that really frustrates and wastes a huge
amount of time.
Any suggestions gratefully received
Steve