Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在列表视图中选择项目?_C#_.net_Winforms_Listview - Fatal编程技术网

C# 如何在列表视图中选择项目?

C# 如何在列表视图中选择项目?,c#,.net,winforms,listview,C#,.net,Winforms,Listview,这是我的代码: 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(File

这是我的代码:

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);
for(int i=0;i<30;i++)
{
添加(新的ListViewItem(新的[]{“asd1”,“asd2”}));
如果(i<10)
{
FileListView.Items[i].Selected=true;
}
}
FileListView.ItemDrag+=新的ItemDragEventHandler(FileListView\u ItemDrag);
但是当我运行应用程序时,我看不到前10个选定项。要查看它们,我需要单击其中一个,它们将高亮显示(但当然会立即取消选择,因为这就像单击一行)


如何预选10个项目?因此,用户可以看到它们被选中,然后单击以拖放到某个目标…

您是使用设计器还是通过代码设置multiselect属性的

 FileListView.MultiSelect=true ;
还可以尝试:

 FileListView.Invalidate() after the loop.

听起来您的ListView没有聚焦,所以当您选择项目时,它们不会高亮显示

您可以像这样在手前对焦控件:

FileListView.Focus();
或者最好禁用该属性。这允许ListView在未聚焦时显示所选项目

FileListView.HideSelection = false;
编辑:使用OPs需要显示为蓝色的新信息,完成后将键盘焦点置于控件上:

FileListView.Select();

正在选择项目,但控件未激活。使用
FileListView.Select()
激活该控件。

我复制/粘贴了您的代码,效果很好。您是否正在运行其他代码来关注/选择表单上的其他元素?我添加了后面的另一行,即链接到ListView的拖动事件。但我不认为这有什么关系…代码在哪里?我已将您的代码复制并粘贴到构造函数中,Form_Load()、Form_Showed()、Form_VisibleChanged(),它们都可以工作。我已将.Focus(在选择它们之前)和Hide selection false设置为.Focus。但现在我看到的是灰色,而不是蓝色!我需要蓝色…对不起,你没有提到他们必须显示蓝色,就好像ListView有键盘焦点一样。更新了答案,但公平地说,我认为@Jon刚刚击败了我!尝试的其他可能性:Control.Invalidate()