How to disable CheckedListBox but allow winforms scrolling
I know this may sound like an easy question, but I can't seem to find a solution. I have a CheckdListBox on my form. I have a list of checkboxes inside. If I do this: clbxMyControl.Enabled = false;
then I cannot scroll to see all the items. How to allow scrolling on a disabled CheckedListBox?
Thank!
source to share
You can prevent the user from checking items with the ItemCheck event:
bool listEnabled = true;
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) {
if (!listEnabled) e.NewValue = e.CurrentValue;
}
But make sure it's obvious that validation no longer works:
private void setListEnabled(bool enabled) {
listEnabled = enabled;
if (listEnabled) checkedListBox1.BackColor = Color.FromKnownColor(KnownColor.Window);
else checkedListBox1.BackColor = Color.FromKnownColor(KnownColor.Control);
}
source to share
ListBox has only one handle in the Windows API, which is configured to be enabled or disabled. So there is no built-in way to scroll, but elements.
You might be able to create your own control with the requested behavior, but you have to draw it from scratch, which can be a lot of work, or you find a third party control that supports the behavior.
Or you cannot use the enabled property, but change the background / font color to make it look disabled and set:
checkedListBox1.SelectionMode = SelectionMode.None;
Another alternative could be DataGridView. It's much more powerful, I'm not sure if it turns off the scrollbar, but if it does, you can make the cells read-only and color them.
source to share