preventing c# listview items from disappearing

genush 41 Reputation points
2024-12-10T20:26:37.2166667+00:00

I have a ListView in a form in c#. There was info about an error in the ListView implementation that causes the items to disappear the first time the mouse hovers over the ListView, and there is a solution to prevent that which works fine. However, my ListView updates the data at 1 Hz and it looks like every time it updates, the items will disappear when the mouse hovers over the ListView. Is there any way to prevent this happening? The solution that works only works when the data displayed in the ListView is static. And if I stop updating, the solution will work.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,169 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
10,352 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jiale Xue - MSFT 48,521 Reputation points Microsoft Vendor
    2024-12-11T07:01:35.45+00:00

    Hi @genush , Welcome to Microsoft Q&A,

    You can temporarily disable ListView during the update process to prevent mouse events from interfering with the update. You can set Enabled = false before the update and restore it after the update:

    listView1.Enabled = false;
    
    listView1.Items.Clear();
    foreach (var item in newData)
    {
        listView1.Items.Add(new ListViewItem(item));
    }
    
    listView1.Enabled = true;
    

    If it doesn't work, please provide a minimal reproducible example Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.