prevent listview to lose selected item

后端 未结 6 1819
失恋的感觉
失恋的感觉 2021-01-18 03:43

I\'m currently working on a listview in winform c# and everytime I click on an empty space on the listview, the selected item is lost.

6条回答
  •  迷失自我
    2021-01-18 04:10

    I accomplished it like this:

    private void lvReads_MouseUp(object sender, MouseEventArgs e)
        {
            if (lvReads.SelectedItems.Count == 0)
                if (lvReads.Items.Count > 0)
                    lvReads.Items.Find(currentName, false)[0].Selected = true;
        }
    

    and

    private void lvReads_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvReads.SelectedItems.Count == 1)
            {               
                selectedIndex = lvReads.SelectedIndices[0];
    
                if (currentName != lvReads.Items[selectedIndex].Name)
                {
    
                    //load item
                }
    
                currentName = lvReads.Items[selectedIndex].Name;
            }
        }
    

提交回复
热议问题