Question Add Items to ListView

naduntha

Member
Joined
Aug 2, 2010
Messages
10
Location
Sri Lanka
Programming Experience
3-5
I used ListView for my new application and i need to update it when new item found.
To do this I used following VB(VB 2010) code.

HTML:
frmMain.ListView1.Items.Add(newItem)
frmMain.ListView1.Refresh()

But, the problem is listview is blinking during the refresh activity. How to overcome this blink matter or please give me a another method to update to ListView.

above code is work for ListBox without blink
Please help.
 
First up, get rid of the call to Refresh. It does nothing useful.

Are you adding multiple items? If so then you should either create them all first and then add them as a batch using AddRange, or else call BeginUpdate on the ListView first and EndUpdate after.
 
First I need to add some amount of items to the ListView and after that i need to and more items to it.
During this process ListView items are blinking. I need to stop this blink. BeginUpdate and EndUpdate also caused the problem
 
If a control "blinks" it is because it is getting redrawn repeatedly in a short space of time. If you're using AddRange or BeginUpdate and EndUpdate then that will specifically prevent the control being redrawn multiple times when adding multiple items. If it's not working for you then it's because you're doing something wrong. As you haven't shown me what you're doing, I have no idea what you're doing wrong, so I can't help you. Please don't make us guess. I assure you that it's very frustrating. Always provide ALL the relevant information and the exact code you're using is ALWAYS relevant. Not just those two lines either because, on their own, they are not capable of making a ListView flicker.
 
I post a example of code like my application's code and this is the very simple version of my code. This code cause the problem. I only added a ListView and a Button for my Form. Please help me.
VB.NET:
Public Class Form1

    Dim list As ListViewItem

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        For Each file In My.Computer.FileSystem.GetFiles("C:\", FileIO.SearchOption.SearchAllSubDirectories)

            ListView1.BeginUpdate()
            list = ListView1.Items.Add(file)
            ListView1.EndUpdate()

        Next

    End Sub

End Class

Thank you
 
Last edited:
BeginUpdate is for batch updates, so using it for each single update is pointless. You could do it like this:
VB.NET:
BeginUpdate
for each file
  add file
next
EndUpdate
or AddRange as suggested:
VB.NET:
Dim newitems As New List(Of ListViewItem)
For Each file In GetFiles
    newitems.Add(New ListViewItem(file))
Next
Me.ListView1.Items.AddRange(newitems.ToArray)
 

Latest posts

Back
Top