ElektroStudios
ElektroStudios

Reputation: 20464

Enable ListBox's ScrollBar even when the ListBox is Disabled?

I have a ListBox full of (predefined) items with multiselect enabled, to prevent human errors I've put a checkbox to lock the Listbox to don't be able to select any item while the checkbox is checked.

Well, what I would like to do is re-enable the ListBox vertical Scrollbar (it's a default control scrollbar) when the listbox is disabled, to let me see the item's that are selected if I want to see them, just to navigate up/down on the Listbox using the scrollbar, just that.

Is this possibly to do?

This is the ListBox, it only has a vertical scrollbar and not horizontal:

enter image description here

Upvotes: 0

Views: 2746

Answers (1)

King King
King King

Reputation: 63327

If you just want to prevent user from interacting with the listbox while still allow him to use the scrollbar, this should do the trick. I've made a custom ListBox which supports some feature to put the listBox into readonly mode:

public class CustomListBox : ListBox
{
    public bool ReadOnly { get; set; }
    protected override void WndProc(ref Message m)
    {
        //WM_LBUTTONDOWN = 0x201
        //WM_KEYDOWN = 0x100
        if (ReadOnly && (m.Msg == 0x201 || m.Msg == 0x100)) {
          Focus();//do this to allow mouse wheeling
          return;
        }
        base.WndProc(ref m);
    }        
}

Usage: You just need to set the ReadOnly property to true:

customListBox1.ReadOnly = true;

Note that it just prevents the left mouse down as well as left mouse click. If you need to prevent user from doing more things than that such as prevent right mouse down, you can catch and filter out the WM_RBUTTONDOWN message.

Upvotes: 3

Related Questions