Click here to Skip to main content
11,703,438 members (75,293 online)
Rate this: bad
good
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 (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()
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:
<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! Smile | :) Smile | :) Smile | :) Smile | :) Smile | :)
Posted 4-Feb-12 17:41pm
sora97625
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Refer this [^]and read the whole conversation.

hope it helps Smile | :)
  Permalink  
Rate this: bad
good
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.

—SA
  Permalink  
v2

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

  Print Answers RSS
0 Maciej Los 540
1 Sergey Alexandrovich Kryukov 400
2 OriginalGriff 340
3 Andy Lanng 124
4 Matt T Heffron 115
0 OriginalGriff 8,519
1 Sergey Alexandrovich Kryukov 7,667
2 CPallini 4,987
3 Maciej Los 4,686
4 Mika Wendelius 3,566


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 5 Feb 2012
Copyright © CodeProject, 1999-2015
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