A list view that automatically updates its data.
To use a list view, first set up the properties. You probably want to set View to Details, and add Columns. It is also is useful to set HideSelection to false.
Now choose a data type for the items in the list. This is your own type, not a special row, and it does not implement a predefined inerface. It should probably be the data type you use for all your business logic. If it implements ToString, text will be automatically displayed. If not, don't worry; you will just have to implement one more event.
The first event to implement is GetItems. Return a collection of your objects. Then if your class doesn't implement ToString (or if you want different text to appear), implement GetItemText. This text is used for the first column of the detail view; for the other columns implement GetSubItems. Return a collection of strings to display in the remaining columns.
Selection in the list view typically represents UI state, not data. You can access the collection of objects that the user has selected with the SelectedItems property.
But sometimes list selection represents data. You might have a collection in your data set that represents a subset of the items in the list, and you want selection to reflect this subset. For better usability, I'd recommend setting the CheckBoxes property and using the GetItemChecked and SetItemChecked events for this. But if it has to be selection, then just implement the GetItemSelected and SetItemSelected events.
Return the business objects to display in the list from GetItems.
If the business object doesn't implement ToString, return the text to display in the first column from GetItemText. Cast the tag parameter to your own data type.
Private Function personListView_GetItems() As System.Collections.IEnumerable
' Return all person objects.
Return _document.People
End Function
Return the strings to display in the remaining columns from GetSubItems. The ToString method is used to convert each object to a string.
Private Function personListView_GetItemText(ByVal tag As Object) As String
' We know that the tag is a person,
' since it was returned from GetItems.
Dim person As Person = DirectCast(tag, Person)
' Display last name, first name in the first column
Return person.LastFirst
End Function
If you have buttons that invoke operations on the selected items of the list, implement their Enabled events to enable them only when some items are selected.
Private Function personListView_GetSubItems(ByVal tag As Object) As System.Collections.IEnumerable
' Display age and occupation in the second and third column.
Dim person As Person = DirectCast(tag, Person)
Dim subItems as New List
subItems.Add(person.Age)
subItems.Add(person.Occupation)
Return subItems
End Function
When you add an object to your collection, it will automatically appear in the list view. But you might want to select it as well.
Private Function ButtonEnabled() As Boolean
' The edit and delete buttons are enabled when a person is selected.
Return personListView.SelectedItems.Count > 0
End Function
Operations that act on selected objects can get them from the SelectedItems collection.
Private Sub addButton_Click(ByVal sender As Object, ByVal e As EventArgs)
' Create the new person object.
Dim person As Person = _document.NewPerson()
' Select the new person object.
personListView.SelectItem(person)
End Sub
Private Sub editButton_Click(ByVal sender As Object, ByVal e As EventArgs)
' Open each selected person.
For Each person As Person In personListView.SelectedItems
EditPerson(person)
Next
End Sub
Private Sub deleteButton_Click(ByVal sender As Object, ByVal e As EventArgs)
' Delete each selected person.
For Each person As Person In personListView.SelectedItems
_document.DeletePerson(person)
Next
End Sub
| New | () |
| CheckedIndices | As System.Windows.Forms.ListView+CheckedIndexCollection | Indices of items in the list view that are checked (read-only). |
| CheckedItems | As System.Collections.IList | Collection of items in the list view that are checked (read-only). |
| Enabled | As Boolean | True if the control is enabled (read-only). |
| Items | As System.Collections.IList | Collection of items in the list view (read-only). |
| SelectedIndices | As System.Windows.Forms.ListView+SelectedIndexCollection | Indices of the selected items in the list view (read-only). |
| SelectedItems | As System.Collections.IList | Collection of items in the list view that are selected (read-only). |
| EditItem | (ByVal tag As Object) | ||
| SelectItem | (ByVal tag As Object) |
| GetEnabled | () | As Boolean | Event fired to determine whether the control is enabled. |
| GetGroupAlignment | (ByVal tag As Object) | As System.Windows.Forms.HorizontalAlignment | Event fired to get the alignment of a group. |
| GetGroupHeader | (ByVal tag As Object) | As String | Event fired to get the header of a group. |
| GetGroupName | (ByVal tag As Object) | As String | Event fired to get the name of a group. |
| GetGroups | () | As System.Collections.IEnumerable | Event fired to get the list of groups. |
| GetItemChecked | (ByVal tag As Object) | As Boolean | Event fired to determine whether an item of a check-box list view is checked. |
| GetItemGroup | (ByVal tag As Object) | As Object | Event fired to get the group to which an item belongs. |
| GetItemImageIndex | (ByVal tag As Object) | As Integer | Event fired to determine the image index of a list view item. |
| GetItemSelected | (ByVal tag As Object) | As Boolean | Event fired to determine whether an item of a multi-select list view is selected. |
| GetItemText | (ByVal tag As Object) | As String | Event fired to get the text associated with an item. |
| GetItems | () | As System.Collections.IEnumerable | Event fired to get the list of items. |
| GetSubItems | (ByVal tag As Object) | As System.Collections.IEnumerable | Event fired to determine the sub items of a list view item. |
| SetItemChecked | (ByVal tag As Object, ByVal value As Boolean) | Event fired when the user checks an item of a check-box list view. | |
| SetItemSelected | (ByVal tag As Object, ByVal value As Boolean) | Event fired when the user selects an item of a multi-select list view. | |
| SetItemText | (ByVal tag As Object, ByVal value As String) | Event fired when the user edits the label of an item. |