Question

I have a listView that lists a bunch of files, and a set of checkboxes that allow users to download them from an FTP server, like so:

Filename            Size    Last modified           New version
[ ] someimage.jpg   120 kB  2010-01-13 16:12:59     Yes
[ ] otherfile.zip   12 kB   2009-12-29 09:33:15     No
[ ] folder      -   -                               -

I don't want to have a checkbox on the folder though, but I do need it (the folder) to be displayed in the list.

How do I best sort this?

Update: I'm using Windows Forms.

Was it helpful?

Solution

you can put your listbox into the OwnerDraw state (set true to the OwnerDraw property). Then define DrawSubItem and DrawColumnHeader event handlers. In the DrawSubItem do default painting for items which do require checkboxes to be drawn. Check the code below; it should skip drawing a checkbox for the item with "1" in its text field and paint only item's text.

private void listView1_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
    if (e.ColumnIndex == 0 && e.Item.Text=="1")
    {
        e.DrawBackground();
        e.DrawText();
    }
    else
    {
        e.DrawDefault = true;
    }
}

private void listView1_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
    e.DrawDefault = true;
}

as an alternative you can do your own checkbox painting by using CheckBoxRenderer class

hope this helps, regards

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top