Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: VS2005 VB8.0 VB.NET
How to refresh listview without loosing selected item?

I have searched google but no luck.... Frown | :( 
I am creating a process explorer like application in visual studio 2005 (
and when i click the refresh button my current selected is gone, now i dont know how to keep the selected item even i click the refresh button
here is my code for the listview:
'list all processes in to a listview
  Private Sub ListProcesses()
        Dim ps() As Process
            ps = Process.GetProcesses()
            lvProcesses.Columns.Add("Name", 100, HorizontalAlignment.Left)
            lvProcesses.Columns.Add("ID", 60, HorizontalAlignment.Left)
            lvProcesses.Columns.Add("Priority", 60, HorizontalAlignment.Right)
            lvProcesses.Columns.Add("Memory(bytes)", 100, HorizontalAlignment.Right)
            Dim p As Process
            For Each p In ps
                Dim lvi As ListViewItem = New ListViewItem()
                lvi.Text = p.ProcessName
            Next p
        Catch e As Exception
        End Try
    End Sub

Dim selectedit As String
selectedit = lvProcesses.SelectedItems(0).Text
Me.lvProcesses.Items(selectedit).Selected = True
'selected item @ listview

source of the listview list code:
<a href=""></a>[<a href="" target="_blank" title="New Window">^</a>]

Thank You!
Any help will be appreciated! Smile | :) Smile | :) Smile | :) Smile | :) Smile | :)
Posted 4-Feb-12 18:41pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Refer this [^]and read the whole conversation.
hope it helps Smile | :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

From your question, I cannot see what exactly your refresh does. No matter; basically, you simply need to remember selection before refresh and set it after refresh. If you insert or delete any items before selection, you should take it into account and shift the index of a row to be selected accordingly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 295
1 DamithSL 265
2 CPallini 235
3 Maciej Los 190
4 Sergey Alexandrovich Kryukov 174
0 OriginalGriff 5,415
1 DamithSL 4,422
2 Maciej Los 3,820
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,911

Advertise | Privacy | Mobile
Web01 | 2.8.141216.1 | Last Updated 5 Feb 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100