markzzz
markzzz

Reputation: 48015

How to select items in a ListView?

This is my code:

for (int i = 0; i < 30; i++)
{
    FileListView.Items.Add(new ListViewItem(new[] { "asd1", "asd2" }));

    if (i < 10)
    {
        FileListView.Items[i].Selected = true;
    }
}

FileListView.ItemDrag += new ItemDragEventHandler(FileListView_ItemDrag);

but when I Run the application, I can't see the first 10 items selected. For see them, I need to click on one of them, and they will highlights (but of course deselected immediatly, since it is like click on a single row).

How can I preselect 10 items? So a user see them selected and then can click to drag/drop to some destination...

Upvotes: 2

Views: 540

Answers (3)

Equalsk
Equalsk

Reputation: 8214

It sounds like your ListView is not focused so when you select the items they won't highlight.

You can either focus the control before hand like this:

FileListView.Focus();

Or what's probably better is to disable the HideSelection property. This allows the ListView to display selected items when not focused.

FileListView.HideSelection = false;

Edit: With OPs new information that they need to show blue, give keyboard focus to the control once you're done:

FileListView.Select();

Upvotes: 2

Jon
Jon

Reputation: 261

The items are being selected but the control is not activated. Use FileListView.Select() to activate the control.

Upvotes: 3

Graffito
Graffito

Reputation: 1718

Did you set the multiselect property with the designer or by code ?

 FileListView.MultiSelect=true ;

Try also:

 FileListView.Invalidate() after the loop.

Upvotes: 0

Related Questions