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!

+3


source to share


4 answers


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);
    }

      

+3


source


Instead of disabling the control, you should change it SelectionMode

like this:

checkedListBox1.SelectionMode = SelectionMode.None;

      



The user will not be able to select the item but will be allowed to scroll

+13


source


This code can be used to scroll the CheckedListBox.

clbxMyControl.SelectionMode = SelectionMode.None;

      

Since Enable = false; make control unscrollable because it makes the checkbox container disabled.

+1


source


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.

+1


source







All Articles