|
Checked List Boxes |
|
Characteristics of a List Box |
|
The Scroll Bars |
|
If you provide a longer list than the list box' height can display, it would have a vertical scroll bar. Here is an example: Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Private lbxFamily As ListBox
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
lbxFamily = New ListBox()
lbxFamily.Location = New Point(12, 12)
lbxFamily.Items.Add("Son")
lbxFamily.Items.Add("Daughter")
lbxFamily.Items.Add("Father")
lbxFamily.Items.Add("Mother")
Dim strMembers As String() = _
{ _
"Niece", "Nephew", "Uncle", "Aunt", _
"Grand Father", "Grand Mother" _
}
lbxFamily.Items.AddRange(strMembers)
Controls.Add(lbxFamily)
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
|
|
This would produce:
At design time, if just one or a few items are hidden by the scroll bar, you can heighten it if the form provides more space. Consider the following example: Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Private lbxBook As ListBox
Private lblTitle As Label
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
lblTitle = New Label()
lblTitle.Text = "Book Titles"
lblTitle.Location = New Point(12, 12)
lbxBook = New ListBox()
lbxBook.Location = New Point(12, 36)
lbxBook.Items.Add("College Algebra")
lbxBook.Items.Add("Finite Mathematics")
lbxBook.Items.Add("Mathematical Structures")
lbxBook.Items.Add("MCAD 70-316 Training Guide")
lbxBook.Items.Add("C++ Builder 6 Developer's Guide")
Controls.Add(lblTitle)
Controls.Add(lbxBook)
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
This would produce:
If at least one of the items of the list box is wider than the width of the control, the right side(s) of that (those) may disappear. To allow the user to see the hidden part of the item(s), you should display a horizontal scroll bar. To support this, the ListBox class is equipped with a Boolean property named HorizontalScrollbar. To make a list box display a horizontal scroll bar, at design time, access the Properties window for the list box and set its HorizontalScrollbar property to True. You can also do this programmatically. Here is an example: Public Sub InitializeComponent()
lblTitle = New Label()
lblTitle.Text = "Book Titles"
lblTitle.Location = New Point(12, 12)
lbxBook = New ListBox()
lbxBook.Location = New Point(12, 36)
lbxBook.Items.Add("College Algebra")
lbxBook.Items.Add("Finite Mathematics")
lbxBook.Items.Add("Mathematical Structures")
lbxBook.Items.Add("MCAD 70-316 Training Guide")
lbxBook.Items.Add("C++ Builder 6 Developer's Guide")
lbxBook.HorizontalScrollbar = True
Controls.Add(lblTitle)
Controls.Add(lbxBook)
End Sub
This property allows the operating system to find the widest item in the list and provide a horizontal scroll bar that is long enough to display each item when the user scrolls to the right. The above code would produce:
If the list of items requires it, the list box would display both the vertical and the horizontal scroll bars. Here is an example: Public Sub InitializeComponent()
lblTitle = New Label()
lblTitle.Text = "Book Titles"
lblTitle.Location = New Point(12, 12)
lbxBook = New ListBox()
lbxBook.Location = New Point(12, 36)
lbxBook.Items.Add("College Algebra")
lbxBook.Items.Add("Finite Mathematics")
lbxBook.Items.Add("Mathematical Structures")
lbxBook.Items.Add("MCAD 70-316 Training Guide")
lbxBook.Items.Add("C++ Builder 6 Developer's Guide")
lbxBook.Items.Add("La Bible de Jérusalem")
lbxBook.Items.Add("Patterns for a Purpose")
lbxBook.HorizontalScrollbar = True
Controls.Add(lblTitle)
Controls.Add(lbxBook)
End Sub
This would produce:
If you prefer to decide how much width should be allowed, then set the desired value in the HorizontalExtent property.
When you create a list of items, they appear in one column. If the number of items exceeds the height, a scrollbar would appear on the control. An alternative you can use is to span the list to more than one column. To support this, the ListBox class is equipped with the MultiColumn Boolean property. At design time, you can set this characteristic in the Properties window. By default, the MultiColumn value is set to False, which means the items appear in one column. If you set this property to True, then the compiler would decide if or when the control needs the columns, based on the number of items in the list. You can then specify the width of each column using the ColumnWidth property.
A list box is painted based on three types or style. This characteristic is controlled by the DrawMode property. When its value is set to Normal, the operating system would regularly draw each item of the list. If you want each item of the list to display a graphic or a color, you must set the style to an owner drawn type. The OwnerDrawFixed value allows you to set a desired but same height for each item of the list. This height is controlled through the ItemHeight property. You can set a different height for each item if you set the list style to OwnerDrawVariable.
A checked list box is a list box whose items are each equipped with a check box. In the following Customize dialog box of Microsoft Visual Studio, the control under the Toolbars label is a checked list box: ![]() A checked list box combines the functionalities of the list box and the check box controls. As a list box, it displays each of its items on a line. If there are too many items than the control can display, it would be equipped with a vertical scroll bar. To select an item in the list, the user can click the desired string. The most important and obvious characteristic of the checked list box is that each item displays a check box on its left. This box allows the user to select or deselect each item. To select an item, the user must click its box and not its string, to indicate an explicit selection. This draws a check mark in the box. As described with the check box control, the user can deselect an item by removing the check mark. The check mark indicates that an item is selected and the absence of the check mark indicates the contrary. Like the check box control, you can allow the user to indicate a "half-checked" item. In this case, a check box can appear unchecked, checked, or grayed.
To support checked list boxes, the .NET Framework provides the CheckedListBox class.
At design time, to add a checked list box to your application, from the Common
Controls section of the Toolbox, click the CheckedListBox button
Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
private lbxPersonalRelationships as CheckedListBox
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
lbxPersonalRelationships = New CheckedListBox()
lbxPersonalRelationships.Location = New Point(12, 12)
Controls.Add(lbxPersonalRelationships)
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
This would produce:
The CheckedListBox class is derived from the ListBox class. This means that the checked list box possesses all the public (and protected) characteristics of the list box and its parent the ListControl class. This control also uses the HorizontalScrollbar and the HorizontalExtent properties that behave exactly as we reviewed for the list box. It also uses the SelectionMode property with the same behavior as that of the list box.
As seen for the list box, the primary aspect of a checked list box is its list of items. At design time, to create the list of items of a checked list box, access its Properties window, and click the ellipsis button to open the String Collection Editor. Type each item followed by a carriage return. After creating the list, click OK. To programmatically create the list of items, access the Items property. The list is created from the nested ObjectCollection class that implements the IList, the ICollection, and the IEnumerable interfaces. This means that the CheckedListBox.ObjectCollection class behaves the same as the ListBox.ObjectCollection class. Here is an example: Public Sub InitializeComponent()
lbxPersonalRelationships = New CheckedListBox()
lbxPersonalRelationships.Location = New Point(12, 12)
lbxPersonalRelationships.Items.Add("Family Member")
lbxPersonalRelationships.Items.Add("Friend")
lbxPersonalRelationships.Items.Add("Classmate")
lbxPersonalRelationships.Items.Add("Business Partner")
lbxPersonalRelationships.Items.Add("Simple Acquaintance")
lbxPersonalRelationships.Items.Add("Undefined")
Controls.Add(lbxPersonalRelationships)
End Sub
This would produce:
Remember that you can also add an array of items by calling the AddRange() and you can insert an item using the Insert() method.
As mentioned already, the main difference between a list box and a checked list is the presence of check marks in the former. When using the control, the user can click one or more check boxes. Here is an example:
After the user has clicked a few check boxes, you may want to find out which ones are checked. The checked list box provides two techniques you can use. As seen for the list box, each item of the control has an index. When one, a few, or all items have been checked (those that display a check mark), the indices of the checked items are stored in a collection represented by the CheckedIndices property. This property is based on the nested CheckedIndexCollection collection class. The CheckedIndexCollection class implements the IList, the ICollection, and the IEnumerable interfaces. The identifications of the checked items are stored in a collection represented by the CheckedItems property. This property is based on the nested CheckedItemCollection class. The CheckedItemCollection class implements the IList, the ICollection, and the IEnumerable interfaces. This implies that you can use it to get the number of selected items. When the user has clicked item to put a check mark on it, the control fires the ItemCheck event. As you can see, the event is carried by the ItemCheckEventArgs class. One of the most important operations to perform on a list of selected items is to find out whether a particular item is checked. To get this information, the CheckedItemCollection class provides a method named Contains. Its syntax is: Public Function Contains(item As Object) As Boolean This method takes as argument a value that can identify an item from the checked list box. If the item is found and it is checked, this method returns true. Otherwise, it returns false.
From its default implementation, when a checked list box is presented to the user, when an item is clicked, its string gets highlighted but no check mark is put in the box. To put a check mark on the item, the user must click it again. This is not an anomaly. It is purposely so the user would know the difference between an item that is selected and one that is checked. To support the ability to automatically put a check mark on an item when the user clicks it, the CheckedListBox provides the CheckOnClick Boolean property. Its default value is False. If you want the items to be automatically checked, set this property to true. On an existing checked list box, to find out if the its items are automatically checked, get the value of the CheckOnClick property.
After creating the list, each item appears with a flat check box to its left. If you want a 3-D check box, you can change the Boolean ThreeDCheckBoxes property from its False default to a True value:
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||
| Previous | Copyright © 2008-2009, yevol.com | Next |
|
|
||