Click here to Skip to main content
12,076,586 members (47,409 online)
Rate this:
Please Sign up or sign in to vote.
See more: VS2005 VB8.0 VB Visual-Studio , +
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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.160208.1 | Last Updated 5 Feb 2012
Copyright © CodeProject, 1999-2016
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