how to highlight an item in listview?

2019-02-23 08:42发布

I'm trying to Drag from listView1 and drop on listLocal which both of them are ListView
It's a file transfer application between client and server, the application shows small local file explorer listLocal and remote file explorer 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.
but it doesn't work, I tried to do listLocal.Focus and listLocal.Select still not working, how could I make it work ?

note : when I used 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;
    }

2条回答
一夜七次
2楼-- · 2019-02-23 09:08

Set the HideSelection property to False

查看更多
贪生不怕死
3楼-- · 2019-02-23 09:17

You may try to handle the DragOver method. or have a look at example from microsoft: MSDN

查看更多
登录 后发表回答