The ListView Class Common Controls and Components

195 ListBox.ObjectCollection.IndexOfvalue where value is an Object representing the item to locate in the collection. The method returns the items index. If the item cannot be found, the method returns -1. Insert Inserts an item into the ListBox at the specified index position. Its syntax is: ListBox.ObjectCollection.Insertindex, item where index is the zero-based ordinal position at which the item is to be inserted, and item is an Object containing the data to be inserted into the collection. Remove Removes the item that is passed as an argument to this method from the ListBox. Its syntax is: ListBox.ObjectCollection.Removevalue where value is an Object representing the item to remove from the collection. RemoveAt Removes an item at the specified index position. Its syntax is: ListBox.ObjectCollection.RemoveAtindex where index is the zero-based ordinal position in the collection of the item to be removed.

5.1.11 The ListView Class

A ListView is a container control that can hold a collection of items. Each item in a ListView can have descriptive text and an image, and the items can be viewed in four modes. The righthand pane of Windows Explorer is a ListView control. An item in a ListView is represented by an object of type ListViewItem. The various constructors of the ListViewItem class permit a ListViewItem to be constructed with a String or with a String and an index number. If an index number is used, it represents the index of the items image in the ImageList referenced by the ListView. The following code constructs two ListViewItem objects. The first has the text Item1 and uses the first image in the ImageList. The second has the text Item2 and uses the second image of the ImageList: Dim listViewItem1 As ListViewItem = New ListViewItemItem1, 0 Dim listViewItem2 As ListViewItem = New ListViewItemItem2, 1 Once you have references to one or more ListViewItem objects, you can add the items to your ListView object. To add an item or a group of items, you first need to reference the ListView.ListViewItemCollection collection of the ListView object. This collection can easily be referenced using the Items property of the ListView class. The ListView.ListViewItemCollection has Add and AddRange methods that you can use to add one item or a group of items. For instance, the following code uses the AddRange method to add two ListViewItem objects to a ListView object: 196 listView1.Items.AddRangeNew ListViewItem _ {listViewItem1, listViewItem2} The Add method of the ListView.ListViewItemCollection has three overloads, two of which allow you add to a ListViewItem without first creating a ListViewItem object. To add a ListViewItem object to the collection, you can use the following overload of the Add method: Overridable Overloads Public Function Add _ ByVal value As ListViewItem _ As ListViewItem Or, to add a String and convert it into a ListViewItem object, use the following overload: Overridable Overloads Public Function Add _ ByVal text As String _ As ListViewItem Alternatively, you can pass a String and an image index to the third overload: Overridable Overloads Public Function Add _ ByVal text As String, _ ByVal imageIndex As Integer _ As ListViewItem The following code demonstrates how to add two ListViewItem objects to a ListView. The ListView is linked to an ImageList that has two images in its collection. When the code is run, it produces something similar to Figur e 5- 4 . Figure 5-4. A ListView control with two ListViewItem objects Declare and instantiate an ImageList called imageList1. Dim imageList1 As ImageList = New ImageList Set the ColorDepth and ImageSize properties of imageList1. imageList1.ColorDepth = ColorDepth.Depth8Bit imageList1.ImageSize = New System.Drawing.Size48, 48 Add two images to imageList1. imageList1.Images.AddNew Iconc:\Spotty.ico imageList1.Images.AddNew Bitmapc:\StopSign.bmp Declare and instantiate two ListViewItem objects named listViewItem1 and listViewItem2. The text for listItem1 is Item1, and the image is the first 197 image in the imageList1. The text for listItem1 is Item2, and the image is the second image in the imageList1. Dim listViewItem1 As ListViewItem = New ListViewItemItem1, 0 Dim listViewItem2 As ListViewItem = New ListViewItemItem2, 1 Declare and instantiate a ListView called listView1. Dim listView1 As ListView = New ListView Set its properties. listView1.View = View.LargeIcon listView1.LargeImageList = imageList1 listView1.Location = New System.Drawing.Point16, 16 listView1.Name = ListView1 listView1.Size = New System.Drawing.Size264, 224 listView1.SmallImageList = Me.ImageList1 Add listViewItem1 and listViewItem2. listView1.Items.AddRangeNew ListViewItem _ {listViewItem1, listViewItem2} Add listView1 to the form. Me.Controls.AddRangeNew Control {listView1} Two properties of the ListView class tell you which items are selected: SelectedIndices and SelectedItems. The first returns a ListView.SelectedIndexCollection object, and the second returns a ListView.SelectedListViewItemCollection object. The ListView.SelectedIndexCollection class has a Count property that tells you how many items are selected and an Item property that returns the index of the designated item. For example, you can retrieve the index of the first selected item by passing 0 to the Item property, as follows: Index = ListView1.SelectedIndices.Item0 The ListView.SelectedListViewItemCollection class is very similar to ListView.SelectedIndexCollection. Its Count property indicates how many items are selected. However, its Item property returns the item itself, not an index number.

5.1.12 The MonthCalendar Class