2

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...

markzzz
  • 47,390
  • 120
  • 299
  • 507
  • I copy/pasted your code and it worked just fine. Are you running any other code to focus/select other elements on the form? [example](http://i.imgur.com/OBqSmxb.png) – Jon Jul 17 '15 at 14:15
  • I've added the other line I have later, the Drag event linked to the ListView. But I don't think that's matter... – markzzz Jul 17 '15 at 14:27
  • Where is this code? I've copied and pasted your code into the constructor, Form_Load(), Form_Shown(), Form_VisibleChanged() and they all work. – Shar1er80 Jul 17 '15 at 14:37

3 Answers3

3

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

Jon
  • 261
  • 1
  • 6
2

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();
Equalsk
  • 7,954
  • 2
  • 41
  • 67
  • I've both .Focus (before select them) and Hide selection false. But now I see the selected items gray, not blue! I need blue... – markzzz Jul 17 '15 at 14:21
  • Sorry, you didn't mention that they had to show blue as if the ListView had keyboard focus. Updated answer, although to be fair I think @Jon just beat me to it! – Equalsk Jul 17 '15 at 14:44
0

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

 FileListView.MultiSelect=true ;

Try also:

 FileListView.Invalidate() after the loop.
Graffito
  • 1,658
  • 1
  • 11
  • 10