Click here to Skip to main content
15,881,172 members

How to refresh listview without loosing selected item?

sora97 asked:

Open original thread
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! :) :) :) :) :)
Tags: Visual Basic (Visual Basic 8 (2005)), Visual Studio (Visual Studio 2005)

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



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