ariel
ariel

Reputation: 16140

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?

Upvotes: 11

Views: 28585

Answers (5)

Ana
Ana

Reputation: 61

use this or set the displaymode to view insted of edit!

public void SetItemEnabled(ListViewItem item, bool enabled)
{
  if (item != null)
  {
      List<ListViewControl> lvControls =  this.ListViewControls.FindAll(FindListViewControl(item));
      foreach (ListViewControl lvControl in lvControls)
    {
       if (lvControl.Control != null)
       {
        lvControl.Control.Enabled = enabled;
        }
        }
    }
}

Upvotes: 0

Leo G.
Leo G.

Reputation: 2139

I took Hans Passant recommendation - good visual approach which in my case denotes un-actionable items. Here's a sample:

    'Select all attachements in case user wants to mask or pick and choose
    For i As Integer = 0 To lstView.Items.Count - 1
        If Not Scan.SupportedMasking.Contains(Path.GetExtension(lstView.Items(i).Text)) Then
            lstView.Items(i).ForeColor = SystemColors.GrayText
            lstView.Items(i).Text += " (No masking supported)"
            lstView.Items(i).BackColor = SystemColors.InactiveBorder
            lstView.Items(i).Selected = False
        Else
            lstView.Items(i).Selected = True
        End If
    Next i

Upvotes: 1

Marginean Vlad
Marginean Vlad

Reputation: 329

You should set the AutoCheck property of the checkbox false.

AutoCheck - Gets or set a value indicating whether the Checked or CheckState values and the CheckBox's appearance are automatically changed when the CheckBox is clicked.

Actually this is usable only for the checkbox control.

Upvotes: -2

Hans Passant
Hans Passant

Reputation: 942488

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

Upvotes: 18

MusiGenesis
MusiGenesis

Reputation: 75396

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.

Upvotes: 4

Related Questions