user1947862
user1947862

Reputation: 13

Disabling checkbox for an item in listview control

I have Checkboxes in my ListView control and set to True. Further, I add items in the ListView one by one. It is not bound to a DataSet / DataTable.

At time, I am adding items with Checkbox.Checked = true in my ListView.

i.e. listview1.Items(i).Checked = True

For some of the items after setting up Checked property to true, I want to disable the Checkbox so that user cannot uncheck the CheckBox.

I am trying to figure out options on how to disable the Checkboxes, but haven't found a solution yet.

Thanks in advance.

Upvotes: 1

Views: 7856

Answers (2)

user2169490
user2169490

Reputation: 31

For me, this works: Handle ListView.ItemCheck Event.

Use the following code there:

private void listView1_BeforeCheck(object sender, ItemCheckEventArgs e)
{
    if (!CheckEnabled(e)) // check somehow if clicked item is disabled
        e.NewValue = e.CurrentValue;
}

Upvotes: 3

Libor
Libor

Reputation: 3313

This is a 3rd party control, Better ListView, but it may interest you. It supports custom check box appearances and hiding (the last two items have check boxes hidden):

enter image description here

To hide a check box on a specific item, simple use:

betterListView.Items[0].CheckBoxAppearance = BetterListViewCheckBoxAppearance.Hide;

Normal usage of check boxes is the same as in .NET ListView, you can have two-state or three-state check boxes or even radio buttons...

Upvotes: 0

Related Questions