How to: retrieve list of selected items in listview with virtual mode on

B

Brian Henry

Since no one else knew how to do this I sat here all morning experimenting
with this and this is what I came up with... Its an example of how to get a
list of items back from a virtual mode list view in .NET 2.0 with multiple
selection turned on... since the .NET documentation is EXTREAML vague on how
to do this and offers no clue on what the virtual mode events do or how to
use them... just thought this might help someone else out...






Public Class Form1

''' <summary>

''' Retrieve thousands of rows of data from database server to fill
dataset to populate list view

''' </summary>

''' <remarks></remarks>

Dim db As New SqlClient.SqlConnection("Data Source=sqlserver;Initial
Catalog=development;Integrated Security=SSPI;Min Pool Size=5;Max Pool
Size=60;Connect Timeout=600;")

Dim da As New
SqlClient.SqlDataAdapter("BENESP_GetJournalEntriesForUser", db)

Dim ds As New DataSet

Dim aItems(0) As ListViewItem



''' <summary>

''' Moves data from data set into an array of list view items (speeds
things up when you preprocess the

''' data set into list view items so you dont have to make them on the
fly when retrieveing virtual items

''' </summary>

''' <remarks></remarks>


Private Sub LoadData()
' Process data items into list view items here and put into array for
easy retrieval
End Sub



''' <summary>

''' Pulls the virtual item out of the list as it is needed to display
on the screen from the

''' array of preprocessed list view items from the initially loaded
data set

''' </summary>

''' <param name="sender"></param>

''' <param name="e"></param>

''' <remarks></remarks>

Private Sub ListView1_RetrieveVirtualItem(ByVal sender As Object,
ByVal e As System.Windows.Forms.RetrieveVirtualItemEventArgs) Handles
ListView1.RetrieveVirtualItem

e.Item = aItems(e.ItemIndex)

End Sub



''' <summary>

''' This happens when a selection is reset to no items selected, like
when a user clicks off a selection

''' </summary>

''' <param name="sender"></param>

''' <param name="e"></param>

''' <remarks></remarks>

Private Sub ListView1_VirtualItemsSelectionRangeChanged(ByVal sender
As Object, ByVal e As
System.Windows.Forms.ListViewVirtualItemsSelectionRangeChangedEventArgs)
Handles ListView1.VirtualItemsSelectionRangeChanged

Debug.WriteLine(String.Format("{3}: Start {0}, End {1}, IsSelected
{2}", e.StartIndex, e.EndIndex, e.IsSelected, Now.ToLongTimeString))

Me.ListBox1.Items.Clear()

End Sub





''' <summary>

''' This happens when a user selects items or multiple selects items
or deselects one of the multi selected items

''' This does not fire though if you have an item selected then click
off it or have multiple items selected then

''' click off them... the virutal selection range change happens at
that point and need to reset your selected

''' items list at that point

''' </summary>

''' <param name="sender"></param>

''' <param name="e"></param>

''' <remarks></remarks>

Private Sub ListView1_ItemSelectionChanged(ByVal sender As Object,
ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs)
Handles ListView1.ItemSelectionChanged

Debug.WriteLine(String.Format("Item Changed: {0}, Is selected: {1}",
e.ItemIndex, e.IsSelected))

If e.IsSelected = True Then

Me.ListBox1.Items.Add(CStr(e.ItemIndex))

Else

Me.ListBox1.Items.Remove(CStr(e.ItemIndex))

End If



End Sub

End Class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top