Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you disable an item in listview control in .net 3.5

In .net 3.5 windows forms I have a listview with "CheckBoxes" = true. Is it possible to dim out or disable some items to prevent the user from checking the box?

like image 704
ariel Avatar asked Mar 29 '11 12:03

ariel


2 Answers

You can use the ListBoxItem.ForeColor and UseItemStyleForSubItems properties to make the item look dimmed. Use SystemColors.GrayText to pick the theme color for disabled items. Avoid disabling selection, it prevents the user from using the keyboard. Only disable the checkbox checking. For example:

    private void listView1_ItemCheck(object sender, ItemCheckEventArgs e) {
        // Disable checking odd-numbered items
        if (e.Index % 2 == 1) e.NewValue = e.CurrentValue;
    }
like image 64
Hans Passant Avatar answered Nov 16 '22 16:11

Hans Passant


You have to roll your own for this. Handle the ListView's ItemSelectionChanged event - if you don't want a particular item to be selectable, do this:

e.Item.Selected = false;

You can make a particular item appear unselectable by graying it out, changing the font color etc.

like image 4
MusiGenesis Avatar answered Nov 16 '22 14:11

MusiGenesis