One of the most obvious characteristics of a column is its title. This is also referred to as its caption. It displays in the column header. The column is represented in TListColumn class by the Caption property: __property System::UnicodeString Caption = {read=FCaption,write=SetCaption}; To visually specify the caption of a column, use the Object Inspector. To do this programmatically, assign the desired string to the TListColumn object. Here is an example: //---------------------------------------------------------------------------
void __fastcall TForm1::btnListViewClick(TObject *Sender)
{
TListView * lvwCountries = new TListView(this);
lvwCountries->Parent = this;
TListColumn * colName = NULL;
lvwCountries->Left = 12;
lvwCountries->Top = 12;
lvwCountries->Width = 350;
lvwCountries->Height = 160;
lvwCountries->ViewStyle = vsReport;
colName = lvwCountries->Columns->Add();
colName->Caption = L"Country Name";
}
//---------------------------------------------------------------------------
By default, the caption of a column aligns to the left. Alternatively, you can position it to the center or the right. This is controlled by the Alignment property: __property Classes::TAlignment Alignment = {read=FAlignment,write=SetAlignment};
The width of a column is controlled by the Width: __property int Width = {read=GetWidth,write=SetWidth};
As reviewed above, the columns of a list view are stored in a TCollection collection. To know the number of columns of a list view, you can check its TCollection::Count property.
If you don't need a column any more, you can delete it. In the same way, you can delete all columns of a list view. To delete a TListColumn object, you can call the TCollection::Delete() method. To delete all columns of a list view, you can call the TCollection::Clear() method.
As mentioned previously, a list view is made of a list of items. An item can be identified by its index:
When a list view comes up, it displays its list of items. To use an item, the user can click it. When an item has been clicked, the control fires an OnChanged event, which is of type TLVChangeEvent: __property TLVChangeEvent OnChange;
When an item has been selected, it becomes highlighted. If you use the LabelEdit property to allow the user to edit an item, this is a local edition. In the next sections, we will learn how to add sub-items to a list view item. When an item is equipped with sub-items, you may want your application to allow the user to change the item, one of its sub-items, or everything. Before doing anything, an item must be activated. There are three ways an item has been activated. The technique used to activate an item is supported by the Activation property. The Activation property is based on the ItemActivation enumeration that has three members:
When an item has been activated, the control fires an ItemActivate event. You can use either this or the SelectedIndexChanged event to process the item.
The idea of having columns is to provide more information about each item of a list view instead of a simple string for each. Consider the following example: //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { TListView * lvwCountries = new TListView(this); lvwCountries->Parent = this; TListItem * lviCountry = NULL; lvwCountries->Left = 12; lvwCountries->Top = 12; lvwCountries->Width = 310; lvwCountries->Height = 120; lvwCountries->ViewStyle = vsReport; TListColumn * colName = lvwCountries->Columns->Add(); colName->Caption = L"Country Name"; colName->Width = 90; TListColumn * colPopulation = lvwCountries->Columns->Add(); colPopulation->Caption = "Population"; colPopulation->Width = 78; colPopulation->Alignment = taRightJustify; TListColumn * colCapital = lvwCountries->Columns->Add(); colCapital->Caption = "Capital"; colCapital->Width = 96; colCapital->Alignment = taLeftJustify; TListColumn * colCode = lvwCountries->Columns->Add(); colCode->Caption = "Code"; colCode->Width = 40; colCode->Alignment = taCenter; lviCountry = lvwCountries->Items->Add(); lviCountry->Caption = L"Egypt"; lviCountry = lvwCountries->Items->Add(); lviCountry->Caption = L"Portugal"; lviCountry = lvwCountries->Items->Add(); lviCountry->Caption = L"Australia"; lviCountry = lvwCountries->Items->Add(); lviCountry->Caption = L"Mali"; lviCountry = lvwCountries->Items->Add(); lviCountry->Caption = L"Sweden"; } //--------------------------------------------------------------------------- This would produce:
To visually create the sub-items, you must use the ListView Item Editor. From that dialog box, click the main item in the left list, then click the New SubItem button.
To support sub-items, the TListItem class is equipped with a property called SubItems. This property is of type TStrings: __property Classes::TStrings * SubItems = {read=FSubItems,write=SetSubItems}; To visually manage the sub-items of an item, you can use the ListView Item Editor. To programmatically manage the sub-items, you can use the properties and methods of the TStrings class, which you should already be familiar with. For example, to create a sub-item, call the Add() method on the SubItems property of a TListItem object. Here are examples: //---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TListView * lvwCountries = new TListView(this);
lvwCountries->Parent = this;
TListItem * lviCountry = NULL;
lvwCountries->Left = 12;
lvwCountries->Top = 12;
lvwCountries->Width = 310;
lvwCountries->Height = 120;
lvwCountries->ViewStyle = vsReport;
TListColumn * colName = lvwCountries->Columns->Add();
colName->Caption = L"Country Name";
colName->Width = 90;
TListColumn * colPopulation = lvwCountries->Columns->Add();
colPopulation->Caption = "Population";
colPopulation->Width = 78;
colPopulation->Alignment = taRightJustify;
TListColumn * colCapital = lvwCountries->Columns->Add();
colCapital->Caption = "Capital";
colCapital->Width = 96;
colCapital->Alignment = taLeftJustify;
TListColumn * colCode = lvwCountries->Columns->Add();
colCode->Caption = "Code";
colCode->Width = 40;
colCode->Alignment = taCenter;
lviCountry = lvwCountries->Items->Add();
lviCountry->Caption = L"Egypt";
lviCountry->SubItems->Add(L"1,001,450");
lviCountry->SubItems->Add("Cairo");
lviCountry->SubItems->Add("eg");
lviCountry = lvwCountries->Items->Add();
lviCountry->Caption = L"Portugal";
lviCountry = lvwCountries->Items->Add();
lviCountry->Caption = L"Australia";
lviCountry = lvwCountries->Items->Add();
lviCountry->Caption = L"Mali";
lviCountry = lvwCountries->Items->Add();
lviCountry->Caption = L"Sweden";
}
//---------------------------------------------------------------------------
This would produce:
In the same way, you can add sub-items to some items and ignore some others.
When you create a new sub-item, it uses a default font and a black color on a white background. If you want, you can change the way a sub-item aesthetically displays. To allow these changes, the list view in the Win32 library provides many functions. To start, you should get a reference to a record (or row) in a list view. For this purpose, the TListItem class is equipped with a property named Handle: __property HWND__ * Handle = {read=GetHandle};
To visually delete an item and its sub-items, in the ListView Item Editor, click the item in the left list and click the Delete button. To programmatically delete an item from a list view, you can call the Delete() method of the TCollection. Here is an example that deletes the third item from the list view: //--------------------------------------------------------------------------- void __fastcall TForm1::btnDeleteItemClick(TObject *Sender) { lvwCountries->Items->Delete(2); } //--------------------------------------------------------------------------- To delete all items from a list view, you can call the TCollection::Clear() method.
If you create the columns of a list view, when the user displays the detail view, the column headers appear and behave like regular buttons. This also means that the user can click a column header and you can take action. If you don't want this appearance and this behavior, you can make the columns appear flat. This characteristics is controlled by the Boolean ColumnClick property of the TCustomListView class: __property bool ColumnClick = {read=FColumnClick,write=SetColumnClick}; When this property is set to True, which is its default value, each column header appears in 3-D:
If the ColumnClick property is True, when the user clicks a column header, it goes down. If you set the ColumnClick property to False, the column headers appear flat and there is no effect if the user clicks them:
To select an item in the list, the user can click it. The selected item indicates this by being highlighted. To select another item, the user can click it and this automatically dismisses the previous selection. You can also allow the user to select an item by positioning the mouse over it. This characteristic is controlled by the HotTrack property of the TCustomListView class: __property bool HotTrack = {read=FHotTrack,write=SetHotTrack}; When an item has been selected, it is represented by the Selected property of the TCustomListView class: __property Comctrls::TListItem * Selected = {read=GetSelected,write=SetSelected};
If you want, you can give the user the ability to select more than one item or you can prevent the user from selecting more than one item. This characteristic is controlled by the MultiSelect property of the TCustomMultiSelectListControl class: __property bool MultiSelect = {read=FMultiSelect,write=SetMultiSelect}; The default value of this property is False, which allows the user to select only one or more items. If you set it to True, the user can select many items at a time. When many items have been selected, the selected items are stored in a list that starts at the Selected property. This means that the first selected item is the Selected object. To get the second item, call the GetNextItem() method of the TCustomListView class. Its syntax is: Comctrls::TListItem * __fastcall GetNextItem(Comctrls::TListItem * StartItem, Comctrls::TSearchDirection Direction, System::Set<Comctrls::TItemState,0,5> States);
After selecting an item, if the user clicks another control, the item that was selected would not be highlighted anymore. If you want the control to continue showing the current selection even when the list view loses focus, set the value of the HideSelection Boolean property of the TCustomListView class: __property bool HideSelection = {read=FHideSelection,write=SetHideSelection};
By default, to select an item, the user must click the item itself and not one of its sub-items. To allow the user to select a an item and its sub-items at the same time, the TCustomListView class provides the FullRowSelect Boolean property: __property bool RowSelect = {read=FRowSelect,write=SetRowSelect}; Its default value is set to false, which obliges the user to click the item itself. If you set this property to true, the whole row would be highlighted when either you or the user selects it. Here is an example of setting this property: //---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TListView * lvwCountries = new TListView(this);
lvwCountries->Parent = this;
lvwCountries->RowSelect = True;
}
//---------------------------------------------------------------------------
When using the report view, to make a list view more indicative, you can show is as a table, made of grid lines. This characteristic is controlled by the GridLines Boolean property of the TCustomListView class: __property bool GridLines = {read=FGridLines,write=SetGridLines}; The default value of this property is false. If you set it to true, grid lines would appear among items throughout the list view, including empty rows. Here is an example of setting this property: //---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TListView * lvwCountries = new TListView(this);
lvwCountries->Parent = this;
lvwCountries->GridLines = True;
}
//---------------------------------------------------------------------------
Besides, or instead of, icons, you can display check boxes with the items of a list view. This characteristic is controlled by the CheckBoxes property: __property bool Checkboxes = {read=FCheckboxes,write=SetCheckboxes}; The default value of this property is False, which omits displaying the check boxes. If you set it to True, a check box would appear on the left of each item of the list view: //---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TListView * lvwCountries = new TListView(this);
lvwCountries->Parent = this;
TListColumn * colCountry = NULL;
TListItem * lviCountry = NULL;
lvwCountries->RowSelect = True;
lvwCountries->GridLines = True;
lvwCountries->Checkboxes = True;
lvwCountries->Left = 12;
lvwCountries->Top = 12;
lvwCountries->Width = 350;
lvwCountries->Height = 110;
lvwCountries->ViewStyle = vsReport;
TListColumn * colName = lvwCountries->Columns->Add();
colName->Caption = L"Country Name";
colName->Width = 90;
colCountry = lvwCountries->Columns->Add();
colCountry->Caption = L"Area (sq km)";
colCountry->Width = 75;
colCountry->Alignment = taRightJustify;
colCountry = lvwCountries->Columns->Add();
colCountry->Caption = L"Population";
colCountry->Width = 70;
colCountry->Alignment = taRightJustify;
TListColumn * colCapital = lvwCountries->Columns->Add();
colCapital->Caption = L"Capital";
colCapital->Width = 70;
colCapital->Alignment = taLeftJustify;
TListColumn * colCode = lvwCountries->Columns->Add();
colCode->Caption = L"Code";
colCode->Width = 40;
colCode->Alignment = taCenter;
lviCountry = lvwCountries->Items->Add();
lviCountry->Caption = L"Egypt";
lviCountry->SubItems->Add(L"1,001,450");
lviCountry->SubItems->Add(L"80,471,869");
lviCountry->SubItems->Add(L"Cairo");
lviCountry->SubItems->Add(L"eg");
lviCountry = lvwCountries->Items->Add();
lviCountry->Caption = L"Portugal";
lviCountry->SubItems->Add("92,391");
lviCountry->SubItems->Add("10,735,765");
lviCountry->SubItems->Add("Lisbon");
lviCountry->SubItems->Add("pt");
lviCountry = lvwCountries->Items->Add();
lviCountry->Caption = L"Australia";
lviCountry->SubItems->Add("7,741,220");
lviCountry->SubItems->Add("21,515,754");
lviCountry->SubItems->Add("Canberra");
lviCountry->SubItems->Add("au");
lviCountry = lvwCountries->Items->Add();
lviCountry->Caption = L"Mali";
lviCountry->SubItems->Add("1,240,192");
lviCountry->SubItems->Add("13,796,354");
lviCountry->SubItems->Add("Bamako");
lviCountry->SubItems->Add("ml");
lviCountry = lvwCountries->Items->Add();
lviCountry->Caption = L"Sweden";
lviCountry->SubItems->Add("450,295");
lviCountry->SubItems->Add("9,074,055");
lviCountry->SubItems->Add("Stockholm");
lviCountry->SubItems->Add("se");
}
//---------------------------------------------------------------------------
This would produce:
Alternatively, to display check boxes on the list view, send a LVM_SETEXTENDEDLISTVIEWSTYLE message to the control. Here is an example: //---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
SendMessage((HWND)lvwCountries->Handle,
(UINT)LVM_SETEXTENDEDLISTVIEWSTYLE,
(WPARAM)(DWORD) LVS_EX_CHECKBOXES,
(LPARAM)LVS_EX_CHECKBOXES);
}
//---------------------------------------------------------------------------
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|