When a WinForm element is disabled, it sort of grays out. Is it possible to disable an element, but adjust the disabled style so it still looks enabled (not grayed out)?
+1
A:
The disabled style is part of standard Windows behavior. If you want to change the style, you'll have to draw the control yourself, meaning that you'll have to handle the Paint
method, and possibly have to override OnPaint
.
See Overriding the OnPaint Method and Custom Control Painting and Rendering.
Jim Mischel
2010-09-27 16:12:09
Do you have any suggestions for disabling a RichTextBox? I'm using one for it's autosize features, but don't want users to be able to mess around with the contained data. Is there a better way to disable the RichTextBox, but make it look like a regular label? (maybe adding an event?)
Soo
2010-09-27 16:14:02
If you don't want users to edit the text set `myRichTextBox.IsReadOnly = true;`
Jim Mischel
2010-09-27 16:21:32
Tiny correction: myRichTextBox.ReadOnly = true; Also, I can still click inside of the box and read text. Is there any way to make it impossible to even click in (without disabling). Thanks though, this did help, and is a step in the right direction.
Soo
2010-09-27 16:25:32
According to the documentation for `CanFocus`, the control and all of its parents must have the `Enabled` property set. You might be able to put the control on a `Panel` that has the `Enabled` property set to `false`. Don't know if that'll show the child disabled, though.
Jim Mischel
2010-09-27 16:33:53
+1
A:
Preventing a focusable control from taking the focus takes a number of counter-measures. You will have to include a control that does take the focus for this class to be resist all attempts:
using System;
using System.Windows.Forms;
class RichLabel : RichTextBox {
public RichLabel() {
this.ReadOnly = true;
this.TabStop = false;
this.SetStyle(ControlStyles.Selectable, false);
}
protected override void OnEnter(EventArgs e) {
if (!DesignMode) this.Parent.SelectNextControl(this, true, true, true, true);
base.OnEnter(e);
}
protected override void WndProc(ref Message m) {
if (m.Msg < 0x201 || m.Msg > 0x20e)
base.WndProc(ref m);
}
}
Hans Passant
2010-09-27 17:42:34