The MonthCalendar Class Common Controls and Components

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

The MonthCalendar class represents a control that displays days of a month. A MonthCalendar control is shown in Figur e 5- 5 . By default, when first displayed, the control displays the current month on the users computer system. Users can select a day by clicking on it or select a range of dates by holding the Shift key while clicking the date at the end of the desired range. Users can also scroll backward and forward to previous or upcoming months, or they can click on the month part and more quickly select one of the 12 months. To change the year, users can click on the year part and click the scrollbar that appears. Figure 5-5. A MonthCalendar control 198 Two properties determine the date range that users can select: MinDate and MaxDate. The MinDate property is a DateTime value representing the minimum date permitted; its default is January 1, 1753. The MaxDate property determines the maximum date allowed. By default, the value of MaxDate is December 31, 9998. If you want your MonthCalendar to display a certain range of dates, you need to change these two properties. For instance, the following code allows the user to select a date between January 1, 1980 and December 14, 2010: MonthCalendar1.MinDate = New DateTime1980, 1, 1 MonthCalendar1.MaxDate = New DateTime2010, 12, 14 The MonthCalendar class has a TodayDate property that represents todays date. The user selecting a new date does not automatically change the value of TodayDate. If you want the date selected by the user to be reflected as todays date, you can use the Date_Changed event handler to change its value explicitly, as shown in the following code: Private Sub MonthCalendar1_DateChanged _ ByVal sender As System.Object, _ ByVal e As DateRangeEventArgs _ Handles MonthCalendar1.DateChanged MonthCalendar1.TodayDate = e.Start End Sub A DateRangeEventArgs object is passed as an argument to the DateChanged event handler. Its members include a Start property, which represents the beginning of the range of selected dates, and an End property, which represents the end of the range of selected dates. The previous code simply assigns the value of the Start property to TodayDate. Later, if you need to know the value of the user- selected date, you can query the TodayDate property. Note that the MonthCalendar control has a fixed size. It will ignore any attempt to change its Size property. If you need more flexibility in terms of the space it occupies, use a DateTimePicker control.

5.1.13 The Panel Class