Click here to Skip to main content
15,885,920 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to refresh listview without loosing selected item?


I have searched google but no luck.... :(

I am creating a process explorer like application in visual studio 2005 (vb.net)
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
      Try
          ps = Process.GetProcesses()
          lvProcesses.BeginUpdate()
          lvProcesses.Clear()
          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
              lvi.SubItems.Add(p.Id.ToString())
              lvi.SubItems.Add(p.BasePriority.ToString())
              lvi.SubItems.Add(p.WorkingSet64.ToString())
              lvProcesses.Items.Add(lvi)
          Next p
          lvProcesses.EndUpdate()
      Catch e As Exception
          MessageBox.Show(e.Message)
      End Try
  End Sub



'refreshbtn
ListProcesses()

SQL
Dim selectedit As String
selectedit = lvProcesses.SelectedItems(0).Text
Me.lvProcesses.Items(selectedit).Selected = True


'listview
'selected item @ listview
lvProcesses.SelectedItems(0).SubItems(0).Text



source of the listview list code:
XML
<a href="http://www.java2s.com/Tutorial/VB/0140__Development/ListallprocessinaListView.htm">http://www.java2s.com/Tutorial/VB/0140__Development/ListallprocessinaListView.htm</a>[<a href="http://www.java2s.com/Tutorial/VB/0140__Development/ListallprocessinaListView.htm" target="_blank" title="New Window">^</a>]




Thank You!
Any help will be appreciated! :) :) :) :) :)
Posted
Updated 29-Sep-18 11:46am

Refer this [^]and read the whole conversation.

hope it helps :)
 
Share this answer
 
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.

—SA
 
Share this answer
 
v2
Try this

Dim selectedit As Integer
selectedit = lvProcesses.FocusedItem.Index

ListProcesses()

lvProcesses.Items.Item(selectedit).Focused = True
lvProcesses.Items.Item(selectedit).Selected = True

Bruno.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900