Creating readonly controls - ComboBox, etc.
The following panel makes any control within it read-only (e.g. unresponsive to clicks/keyboard input) without turning it dark grey.
There is an alternate solution specifically for combobox as presented at the PDC. Code is here.
public class ReadonlyControlHolder : Panel {
private bool readOnly = false;
public bool ReadOnly {
get {
return readOnly;
}
set {
if (readOnly != value) {
readOnly = value;
if (IsHandleCreated) {
SafeNativeMethods.EnableWindow(new HandleRef(this, this.Handle), !readOnly);
}
}
}
}
protected override void OnHandleCreated(EventArgs e) {
base.OnHandleCreated(e);
if (ReadOnly) {
SafeNativeMethods.EnableWindow(new HandleRef(this,this.Handle), !readOnly);
}
}
private class SafeNativeMethods {
[DllImport("user32")]
public static extern void EnableWindow(HandleRef hwnd, bool enabled);
}
}