简体   繁体   English

如何突出listview中的项目?

[英]how to highlight an item in listview?

I'm trying to Drag from listView1 and drop on listLocal which both of them are ListView 我正试图从listView1拖动并放在listLocal 上,它们都是ListView
It's a file transfer application between client and server, the application shows small local file explorer listLocal and remote file explorer listView1 . 它是客户端和服务器之间的文件传输应用程序,应用程序显示小型本地文件资源管理listLocal和远程文件资源管理器listView1
so when I drop the items from listView1 to listLocal and the pointer points on an item[ Folder ] it should be highlighted item.Selected = true . 因此,当我将项目从listView1拖放到listLocal并且指针指向项目[ Folder ]时,它应突出显示item.Selected = true
but it doesn't work, I tried to do listLocal.Focus and listLocal.Select still not working, how could I make it work ? 但它不起作用,我试图做listLocal.FocuslistLocal.Select仍然无法正常工作,我怎么能让它工作?

note : when I used item.BackColor = Color.RoyalBlue; 注意:当我使用item.BackColor = Color.RoyalBlue; it worked, but it doesn't highlight the icon. 它工作,但它没有突出显示图标。

   private void listLocal_DragOver(object sender, DragEventArgs e)
   {
      if (!e.Data.GetDataPresent(typeof(ListViewItem))) return;
      Point p = listLocal.PointToClient(MousePosition);
      ListViewItem targetItem = listLocal.GetItemAt(p.X, p.Y);
      if (targetItem != null)               //if dropping on a target item
      {
        targetItem.Selected = true;
        if (targetItem.SubItems.Count > 1) e.Effect = DragDropEffects.None;//if IsFile
        else e.Effect = DragDropEffects.Copy;
        return;
      }
      foreach (ListViewItem item in listLocal.Items) item.Selected = false; //if dragging into current address
      e.Effect = DragDropEffects.Copy;
    }

将HideSelection属性设置为False

You may try to handle the DragOver method. 您可以尝试处理DragOver方法。 or have a look at example from microsoft: MSDN 或者看一下microsoft的例子: MSDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM