Click here to Skip to main content
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
sora97635
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 CHill60 360
1 OriginalGriff 203
2 Sarvesh Kumar Gupta 168
3 RyanDev 145
4 Peter Leow 145


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