By default, the user can select only one item in the list. The enhance this aspect, the TCustomListBox is equipped with a property named ExtendedSelect: __property bool ExtendedSelect = {read=FExtendedSelect,write=SetExtendedSelect}; The TCustomListBox::ExtendedSelect property is used in combination with the MultiSelect: of the TCustomMultiSelectListControl class: __property bool MultiSelect = {read=FMultiSelect,write=SetMultiSelect}; Both properties are represented in the Object Inspector. The default value of the ExtendedSelect property is True but the default value of the MultiSelect property is False. If you want the user to be able to select more than one item, you should set the MultiSelect property to True since the ExtendedSelect property would already be set to True. After the user has selected more than one item, you can use the TCustomMultiSelectListControl::SelCount to find out the number of items that the user would have selected: __property int SelCount = {read=GetSelCount};
When the items of a list box appear in alphabetical, numerical, or chronological order, the list is said to be sorted. By default, the items of a list box are not sorted. To let you arrange the list, the TCustomListBox class is equipped with a Boolean property named Sorted: __property bool Sorted = {read=FSorted,write=SetSorted}; During design, you can use the Objects Inspector to specify the value of this property. As a Boolean data type, you can also set the sorting feature programmatically as follows: //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { Listing->Sorted = True; } //--------------------------------------------------------------------------- If you create an unsorted list, then at one time get it sorted (for example, you can give the user the ability to sort the list, by clicking a button), the list would be sorted. If an item is added to the sorted list, the compiler would automatically insert that item to the right position following the order. If at another time you allow the user to "unsort" the list, the list would keep its current order. If another item is added when the list is not sorted, the item would be positioned at the end of the list. If you want the list to have its original state, you would have to reset it through code.
The list boxes are designed in three types of style. The default is the lbStandard in which case each item in the list is an AnsiString object. On the other hand, if you want each item of the list to display a graphic or a color, you must set the style to an owner draw. The lbOwnerDrawFixed allows you to set a desired 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 lbOwnerDrawVariable.
To delete the 4th item of the list, you could write: //--------------------------------------------------------------------------- void __fastcall TForm1::btnDelete4Click(TObject *Sender) { lstCountries->Items->Delete(3); } //--------------------------------------------------------------------------- If the list contains less than 4 items, the TStrings::Delete() method would ignore the operation. If you have an Edit control called edtCountry, you can let the user add its content to the list box when he double-clicks the edit box. The code could look as follows: //--------------------------------------------------------------------------- void __fastcall TForm1::Edit1DblClick(TObject *Sender) { lstCountries->Items->Add(edtCountry->Text); } //--------------------------------------------------------------------------- If the user is filling out a list box while typing additional items in an Edit control, the best and fastest way to add a new item is when the user presses Enter after typing the item. To implement this behavior, you should find out what key the user pressed. If the user presses Enter when the edit box has focus, you should make sure the edit box contains a string. The code could appear like this: //--------------------------------------------------------------------------- void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key) { if( Key == 13 ) // If the user presses the Enter key { if(Edit1->Text != "") // If Edit1 contains something { lstCountries->Items->Add(Edit1->Text); // Add the content of Edit1 to ListBox1 Edit1->Text = ""; // Reset Edit1 and make it empty } else // Since Edit1 is empty, display a message Panel1->Caption = "There is nothing to add"; } } //--------------------------------------------------------------------------- The TCustomListBox::Clear() method is used to clear the control of its whole content: //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { lstCountries->Clear(); } //---------------------------------------------------------------------------
A checked list box is a List Box whose items are each equipped with a check box:
A checked list box combines the functionalities of the List Box and the check box. 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. As described for the list box, if at least one of the items is wider than the control's width, you can make the list box display a horizontal scroll bar.
A checked list box control is based on the TCheckListBox class. To provide a checked list box, from the Additional tab of the Tool Palette, click the TCheckListBox button and click the form or container that would host the control. To programmatically create this control, you can use its constructor. Specify the control owner and its parent. All of the functionality of the checked list box control is provided though its properties.
Like the regular list box, the items of a checked list box are UnicodeString strings controlled by a TStrings property called Items. At design time, to create a list of items and add it to the control, open the String List Editor dialog box, add the necessary strings, and click OK. Of course, you can add the strings at run time, using the Items::Add() method. When you create the list, the items are stored in the same order you entered them, if you want, you can rearrange them alphabetically or numerically. This can be done by setting the Boolean value of the Sorted property accordingly. As described for the list box, if you set it to true, the items of the checked list box would be sorted. If you set it back to false, the items of the list box would not go back to the way they were but new items would be added at the end of the list. If you provide a longer list than the control's height can display, it would have a vertical scroll bar. If just one or a few items are hidden by the scroll bar, you can heighten it if the form provides more space. Alternatively, you can also create the list in various columns. To do this, set the value of the Columns property to a number of your choice. Normally, the number of columns should not exceed 5 or this would indicate that you may simply need more than one checked list box control: If at least one of the items is wider than the width of the control, you have various alternatives. You can display a horizontal scroll bar by calling the Win32 API SendMessage() function and specifying the LB_SETHORIZONTALEXTENT value as the message to send: //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { SendMessage(CheckListBox1->Handle, LB_SETHORIZONTALEXTENT, 140, 0); } //---------------------------------------------------------------------------
To select an item in the list, the user can click the desired string. Unlike the regular list box, the user cannot select more than one string in the list. For this reason, if you desire a normal list of objects, use the regular list box control. When the user clicks the check box of an item, the checked list box fires an OnClickCheck event. You can use this event to take some action. The OnClickCheck event of a TNotifyEvent type and therefore specifies only the Sender of the event. 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, the user can deselect a string 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, you can allow the user to indicate a "half-checked" item. In this case, a check box can appear unchecked, checked, or grayed.
After creating the list, each item appears with a flat check box to its left. This detailt is controlled by the Boolean Flat property: __property bool Flat = {read=FFlat,write=SetFlat}; If you want a 3-D check box, you can change the Flat property from its true default to a false value.
You can also resize the control to make it wider to accommodate the large item. After creating the list, sorted or not, each item has a positional index that allows you to access it. The array of this index can have a different name depending on why you want to access it. For example, the items are stored in an array called Header: __property bool Header = {read=GetHeader,write=SetHeader}; Each item can be accessed using the Header[Index] array. The Header index is used if you want an item to be distinct from the others. To do this, at run time, access that item Header index and set its Header Boolean property to true. To distinguish the header item from the others, it uses a different text color and it does not display a check box. Here is an example: //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { CheckListBox1->Header[2] = True; } //--------------------------------------------------------------------------- You can make more than one item appear as Header.
The Header item displays a color represented by the HeaderBackgroundColor property: __property Graphics::TColor HeaderBackgroundColor = { read=FHeaderBackgroundColor, write=SetHeaderBackgroundColor };
The text of the Header item displays in a color known as the HeaderColor property: __property Graphics::TColor HeaderColor = {read=FHeaderColor,write=SetHeaderColor}; You can set these values to any valid color you want.
The items of a checked list box are stored in an array called Checked: __property bool Checked = {read=GetChecked,write=SetChecked}; The first or top item of a checked list box has an index of 0 and can be accessed with Checked[0]. The second item has an index of 1 and can be accessed with Checked[0], etc. As stated already, to select an item, the user clicks its check box. To find out if an item has been checked, access its Checked index and check whether it is true or false. Here is an example: //--------------------------------------------------------------------------- void __fastcall TForm1::CheckListBox1Click(TObject *Sender) { if( CheckListBox1->Checked[1] == True ) ShowMessage(L"Welcome to Montgomery County!"); } //---------------------------------------------------------------------------
To increase the options of the checked list box, you can let the user "half-Select" an item or indicate when an item is not completely selected and not completely deselected. Such an item is referred to as grayed. This is controlled by the AllowGrayed Boolean property: __property bool AllowGrayed = {read=FAllowGrayed,write=FAllowGrayed};
If a checked list box control has this property on, to check the appearance of a check mark or absence of it on an item, use the State property: __property Stdctrls::TCheckBoxState State = {read=GetState,write=SetState}; The value of this property is an enumeration type defined as follows: enum TCheckBoxState { cbUnchecked, cbChecked, cbGrayed };
The check mark appearance of the items of a checked list box control are stored in an array called State. To find out the check mark or lack of it of an item, call the State property and specify the index of the desired item. The following checks whether a check mark appears on the 5th item of the list: //--------------------------------------------------------------------------- void __fastcall TForm1::CheckListBox1Click(TObject *Sender) { if( CheckListBox1->State[4] == cbChecked ) ShowMessage(L"The Bay Bridge is closed.\nYou can't get to Annapolis!!!"); } //---------------------------------------------------------------------------
If for any reason you do not want the users to be able to select items, only to view the list, you can set the Enabled property of the control to false. On the other hand, if you want to disable only one or a few items, you can do that. The items of a checked list box are stored in an array called ItemEnabled: __property bool ItemEnabled = {read=GetItemEnabled,write=SetItemEnabled}; To enable or disable an item, call this property and specify the index of the item you want to enable or disable. Here is an example: //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { CheckListBox1->ItemEnabled[2] = False; CheckListBox1->ItemEnabled[4] = False; CheckListBox1->ItemEnabled[5] = False; } //--------------------------------------------------------------------------- |
|
|||||||||||||||||||||||||||||||||||||||
|