Listbox resources are described with the LISTBOX structure, as defined in the eikon.rh file.
STRUCT LISTBOX { BYTE version; // version number WORD flags; // listbox flags WORD height; // in items WORD width; // in chars LLINK array_id; // points to items in ARRAY structure }
STRUCT ARRAY { STRUCT items[]; // list items }
The listbox resource definition describes the application’s listbox properties. The resource contains also information about the type of the listbox.
The listbox items are defined as an array. The array r_listbox_items
below
defines the listbox item strings for the listbox resource r_listbox
above.
The listbox resource is defined in the application resource file (RSS file).
The following code describes a selection listbox resource called r_listbox
.
RESOURCE LISTBOX r_listbox { flags = EAknListBoxSelectionList; array_id = r_listbox_items; } RESOURCE ARRAY r_listbox_items { items = { LBUF { txt = "\tItem1\t\t"; }, LBUF { txt = "\tItem2\t\t"; }, LBUF { txt = "\titem3\t\t"; } }; }
The listbox can also be created in a view-only mode, as a viewer listbox,
without the highlight indicator. In order to create a viewer listbox, a flag
called EAknListBoxViewerFlags
must be used in conjunction
with the listbox type when defining the flags parameter for the listbox resource.
The example listbox resource r_listbox_viewer
below demonstrates
how to create a selection list in a viewer mode.
RESOURCE LISTBOX r_listbox_viewer { flags = EAknListBoxSelectionList | EAknListBoxViewerFlags; array_id = r_listbox_items; }
Once the resources have been defined and a resource file created, the listbox resource can be loaded and used in the application.
This Section describes how to create lists from resource files and at run time. If lists are created from a resource, a resource file defining the listbox resource must exist and contain a valid listbox resource definition of the listbox structure and listbox items. If the listbox is created at run time, the listbox item array is constructed and initialized in the code.
When creating a listbox, choose a listbox layout that best suits your needs. The chosen layout specifies the class to be used when creating a listbox and the listbox item string format to be used when creating the listbox items.
Lists can be easily created and used from a resource file defining them. The resource file should define the listbox resource and the listbox resource items array. Create a listbox from a resource by following these steps:
First, an instance of the listbox is created and its container control is set. Next, the resource reader is created to prepare the loading of the resource file. Here, the resource reader and listbox resource identifier are passed as parameters. The listbox resource is referred to, using the name given to identify it when defining the resource in the resource file.
Finally, the listbox resource file is loaded by calling the ConstructFromResourceL()
method
with the resource reader as a parameter. If the resource loading is successful,
the listbox is ready for use.
The following example code creates a selection listbox with a single-line
item layout loaded from a resource file. This code expects the resource file
to define the resource R_LISTBOX
.
In this example a listbox is created, and activated. Note that in the example
listbox is component of a compound control and it is needed to implement method ComponentControl()
and CountComponentControls()
.
class CMyContainer : public CCoeControl { CEikTextListBox* iListBox; } void CMyContainer::ConstructL() { // Create listbox iListBox = new ( ELeave ) CAknSingleStyleListBox(); // markable iListBox->SetContainerWindowL( *this ); // Initialize resource reader, passing resource id as parameter TResourceReader rr; iEikonEnv->CreateResourceReaderLC( rr, R_LISTBOX ); // Load and construct listbox iListBox->ConstructFromResourceL( rr ); CleanupStack::PopAndDestroy(); // rr // Activate listbox iListBox->SetRect( Rect() ); iListBox->ActivateL(); }
Creating lists without resource file definitions is fairly simple. In this approach, the listbox format and contents are set manually at run time as the resources are not used. To create a listbox at run time, follow these steps:
CAknSingleNumberStyleListBox
class.
EAknListBoxSelectionList
to create a selection
list.
The following example code shows how to create a numbered selection listbox
with three items at run time, without using a resource file. Note that the
example listbox is a component of a compound control and it is needed to implement
method ComponentControl()
and CountComponentControls()
.
class CMyContainer : public CCoeControl { CEikTextListBox* iListBox; CDesCArrayFlat* iListBoxItems; } void CMyContainer::ConstructL() { … CreateWindowL(); // This is a window owning control // Create a single numbered style listbox iListBox = new (ELeave) CAknSingleNumberStyleListBox(); // markable // Construct listbox iListBox->ConstructL( this, EAknListBoxSelectionList | EAknListBoxLoopScrolling ); iListBox->SetContainerWindowL( *this ); // Set scrollbars iListBox->CreateScrollBarFrameL( ETrue ); iListBox->ScrollBarFrame()->SetScrollBarVisibilityL( CEikScrollBarFrame::EOn, CEikScrollBarFrame::EAuto ); // Create listbox item array iListBoxItems = new (ELeave) CDesCArrayFlat(4); // Create listbox items iListBoxItems->AppendL( _L("1\tItem") ); iListBoxItems->AppendL( _L("2\tItem") ); iListBoxItems->AppendL( _L("3\tItem") ); // Add items to listbox iListBox->Model()->SetItemTextArray( iListBoxItems ); // Listbox deletes the item array iListBox->Model()->SetOwnershipType( ELbmOwnsItemArray ); iListBox->HandleItemAdditionL( ); // Activate Listbox iListBox->SetRect( Rect() ); iListBox->ActivateL(); … }
Note, that listbox must be informed about size changed events, to display properly.
void CSumoTestContainer::SizeChanged() { … iListBox->SetRect( Rect() ); … }
The previous code example demonstrated how to create a listbox at run time, without a resource file. The next code example demonstrates how to use a large graphic a primary and secondary text in the listbox. To create a listbox from code, follow these steps:
CAknDoubleLargeStyleListBox
class.
EAknListBoxSelectionList
to create a selection
list.
Note that the order of the icons in the icon array defines their indexes.
In this example, the first icon added to the icon list is ndexed as zero (0
),
and the next item, as one (1
). The first column in the listbox
item string defines the index of the icon to be displayed for the specific
listbox item.
The following example code shows how to create a listbox with a large graphic,
a primary and a secondary text at run time. Note that the example listbox
is component of a compound control and it is needed to implement method ComponentControl()
and CountComponentControls()
.
class CMyContainer : public CCoeControl { CEikFormattedCellListBox* iListBox; CDesCArrayFlat* iListBoxItems; } void CMyContainer::ConstructL() { … CreateWindowL(); // This is a window owning control // Create listbox iListBox = new (ELeave) CAknDoubleLargeStyleListBox(); // Construct listbox iListBox->ConstructL( this, EAknListBoxSelectionList | EAknListBoxLoopScrolling ); iListBox->SetContainerWindowL( *this ); // Set scrollbars iListBox->CreateScrollBarFrameL( ETrue ); iListBox->ScrollBarFrame()->SetScrollBarVisibilityL( CEikScrollBarFrame::EOn, CEikScrollBarFrame::EAuto ); // Create listbox item array iListBoxItems = new (ELeave) CDesCArrayFlat(4); // Create listbox items iListBoxItems->AppendL( _L("0\tHeading1\tItem1\t") ); iListBoxItems->AppendL( _L("1\tHeading2\tItem2\t") ); iListBoxItems->AppendL( _L("2\tHeading3\tItem3\t") ); // Add items to listbox iListBox->Model()->SetItemTextArray( iListBoxItems ); // Listbox deletes the item array iListBox->Model()->SetOwnershipType( ELbmOwnsItemArray ); iListBox->HandleItemAdditionL( ); // Add icons to listbox AddListboxIconsL(); // Activate Listbox iListBox->SetRect( Rect() ); iListBox->ActivateL(); … } // This method adds the icons to iListbox void CMyContainer::AddListboxIconsL() { CArrayPtr<CGulIcon>* iconArray = new( ELeave ) CAknIconArray( 1 ); CleanupStack::PushL( iconArray ); CFbsBitmap* addressIcon = NULL; CFbsBitmap* addressIconMask = NULL; CFbsBitmap* emailIcon = NULL; CFbsBitmap* emailIconMask = NULL; CFbsBitmap* faxIcon = NULL; CFbsBitmap* faxIconMask = NULL; AknIconUtils::CreateIconLC( addressIcon, addressIconMask, KAvkonBitmapFile, EMbmAvkonQgn_prop_nrtyp_address, EMbmAvkonQgn_prop_nrtyp_address_mask ); CGulIcon* addressListIcon = CGulIcon::NewL( addressIcon, addressIconMask ); CleanupStack::Pop( 2 ); // addressIcon, addressIconMask CleanupStack::PushL( addressListIcon ); iconArray->AppendL( addressListIcon ); AknIconUtils::CreateIconLC( emailIcon, emailIconMask, KAvkonBitmapFile, EMbmAvkonQgn_prop_nrtyp_email, EMbmAvkonQgn_prop_nrtyp_email_mask ); CGulIcon* emailListIcon = CGulIcon::NewL( emailIcon, emailIconMask ); CleanupStack::Pop( 2 ); // emailIcon, emailIconMask CleanupStack::PushL( emailListIcon ); iconArray->AppendL( emailListIcon ); AknIconUtils::CreateIconLC( faxIcon, faxIconMask, KAvkonBitmapFile, EMbmAvkonQgn_prop_nrtyp_fax, EMbmAvkonQgn_prop_nrtyp_fax_mask ); CGulIcon* faxListIcon = CGulIcon::NewL( faxIcon, faxIconMask ); CleanupStack::Pop( 2 ); // faxIcon, faxIconMask CleanupStack::PushL( faxListIcon ); iconArray->AppendL( faxListIcon ); iListBox->ItemDrawer()->ColumnData()->SetIconArray( iconArray ); // faxListIcon, emailListIcon, addressListIcon, iconArray CleanupStack::Pop( 4 ); iListBox->HandleItemAdditionL(); }
The menu and selection lists allow selection of one item on the list. The listbox instance contains the index of its currently selected item. The index of the currently selected item on the menu and selection lists can be retrieved by the following code:
TInt currentIndex = iListBox->CurrentItemIndex();
In a multiselection list, different list items can be selected at the same
time, to perform some operation on them. A listbox consists of a checkbox
icon, and a text. Because of the type of the listbox items, the CAknSingleGraphicStyleListBox
class
is used, so a checkbox can be used on the left side on the list item. To
create a listbox from code, follow these steps:
CAknSingleGraphicStyleListBox
class.
EAknListBoxMultiselectionList
to create a multiselection
list.
Note that the order of the icons in the icon array defines their indexes. In this example, the first icon is indexed as zero (0), and the next is indexed as one (1). The first column in the listbox item string defines the index of the icon to be displayed for the specific listbox item.
The example listbox is a component of a compound control and it is needed
to implement the method ComponentControl()
and CountComponentControls()
.
class CMyContainer : public CCoeControl { CAknColumnListBox* iListBox; CDesCArrayFlat* iListBoxItems; } void CMyContainer::ConstructL() { … CreateWindowL(); // This is a window owning control // Create listbox iListBox = new (ELeave) CAknSingleGraphicStyleListBox(); // markable // Construct listbox iListBox->ConstructL( this, EAknListBoxMultiselectionList| EAknListBoxLoopScrolling ); iListBox->SetContainerWindowL( *this ); // Set scrollbars iListBox->CreateScrollBarFrameL( ETrue ); iListBox->ScrollBarFrame()->SetScrollBarVisibilityL( CEikScrollBarFrame::EOn, CEikScrollBarFrame::EAuto ); // Create listbox item array iListBoxItems = new (ELeave) CDesCArrayFlat(4); // Create listbox items iListBoxItems->AppendL( _L("1\tItem1") ); iListBoxItems->AppendL( _L("1\tItem2") ); iListBoxItems->AppendL( _L("1\tItem3") ); // Add items to listbox iListBox->Model()->SetItemTextArray( iListBoxItems ); // Listbox deletes the item array iListBox->Model()->SetOwnershipType( ELbmOwnsItemArray ); iListBox->HandleItemAdditionL( ); // Add icons to listbox AddCheckboxIconsL(); // Activate Listbox iListBox->SetRect( Rect() ); iListBox->ActivateL(); … } void CMyContainer::AddCheckboxIconsL() { CAknIconArray* iconArray = new( ELeave ) CAknIconArray( 1 ); CleanupStack::PushL( iconArray ); CFbsBitmap* checkboxOnBitmap = NULL; CFbsBitmap* checkboxOnBitmapMask = NULL; CFbsBitmap* checkboxOffBitmap = NULL; CFbsBitmap* checkboxOffBitmapMask = NULL; //CListItemDrawer is using this logical color as default for its marked icons TRgb defaultColor; defaultColor = CEikonEnv::Static()->Color( EColorControlText ); AknsUtils::CreateColorIconLC( AknsUtils::SkinInstance(), KAknsIIDQgnPropCheckboxOff, KAknsIIDQsnIconColors, EAknsCIQsnIconColorsCG13, checkboxOnBitmap, checkboxOnBitmapMask, KAvkonBitmapFile, EMbmAvkonQgn_indi_checkbox_on, EMbmAvkonQgn_indi_checkbox_on_mask, defaultColor ); CGulIcon* checkboxOnIcon = CGulIcon::NewL( checkboxOnBitmap, checkboxOnBitmapMask ); CleanupStack::Pop( 2 ); // checkboxOnBitmap, checkboxOnBitmapMask CleanupStack::PushL( checkboxOnIcon ); iconArray->AppendL( checkboxOnIcon ); AknsUtils::CreateColorIconLC( AknsUtils::SkinInstance(), KAknsIIDQgnPropCheckboxOff, KAknsIIDQsnIconColors, EAknsCIQsnIconColorsCG13, checkboxOffBitmap, checkboxOffBitmapMask, KAvkonBitmapFile, EMbmAvkonQgn_indi_checkbox_off, EMbmAvkonQgn_indi_checkbox_off_mask, defaultColor ); CGulIcon* checkboxOffIcon = CGulIcon::NewL( checkboxOffBitmap, checkboxOffBitmapMask ); CleanupStack::Pop( 2 ); // checkboxOffBitmap, checkboxOffBitmapMask CleanupStack::PushL( checkboxOffIcon ); iconArray->AppendL( checkboxOffIcon ); iListBox->ItemDrawer()->ColumnData()->SetIconArray( iconArray ); // checkboxOffIcon, checkboxOnIcon, iconArray CleanupStack::Pop( 3 ); }
This example shows how to change the checkbox states of a multiselection list item.
// Checks the second item in the list iListBox->View()->SelectItemL( 1 ); // Unchecks the first item in the list iListBox->View()->DeselectItem( 0 );
A markable listbox is essentially a selection listbox with an added feature
that allows list items to be marked. Any number of items can be marked on
the list. A command can be applied to all of the marked items. A listbox consists
of a mark icon, if the item is marked and a text. Because of the type of the
listbox items, the CAknSingleGraphicStyleListBox
class is
used, so a mark icon can be used on the right side on the list item. To create
a listbox from code, follow these steps:
CAknSingleGraphicStyleListBox
class.
EAknListBoxMarkableList
to create a markable
list.
Note that the order of the icons in the icon array defines their indexes.
In this example, the first icon is indexed as zero (0
), and
the next is indexed as one (1
). The first column in the listbox
item string defines the index of the icon to be displayed for the specific
listbox item.
The example listbox is a component of a compound control and it is needed
to implement the method ComponentControl()
and CountComponentControls()
.
class CMyContainer : public CCoeControl { CAknColumnListBox* iListBox; CDesCArrayFlat* iListBoxItems; } void CMyContainer::ConstructL() { … CreateWindowL(); // This is a window owning control // Create a single numbered style listbox iListBox = new (ELeave) CAknSingleGraphicStyleListBox(); // markable // Construct listbox iListBox->ConstructL( this, EAknListBoxMarkableList | EAknListBoxLoopScrolling ); iListBox->SetContainerWindowL( *this ); // Set scrollbars iListBox->CreateScrollBarFrameL( ETrue ); iListBox->ScrollBarFrame()->SetScrollBarVisibilityL( CEikScrollBarFrame::EOn, CEikScrollBarFrame::EAuto ); // Create listbox item array iListBoxItems = new (ELeave) CDesCArrayFlat(4); // Create listbox items iListBoxItems->AppendL( _L("\tItem1\t\t") ); iListBoxItems->AppendL( _L("\tItem2\t\t") ); iListBoxItems->AppendL( _L("\tItem3\t\t") ); // Add items to listbox iListBox->Model()->SetItemTextArray( iListBoxItems ); // Listbox deletes the item array iListBox->Model()->SetOwnershipType( ELbmOwnsItemArray ); iListBox->HandleItemAdditionL( ); // Add icons to listbox AddMarkIconsL(); // Activate Listbox iListBox->SetRect( Rect() ); iListBox->ActivateL(); … } void CMyContainer::AddMarkIconsL() { CAknIconArray* iconArray = new(ELeave) CAknIconArray(1); CleanupStack::PushL(iconArray); CFbsBitmap* markBitmap = NULL; CFbsBitmap* markBitmapMask = NULL; //CListItemDrawer is using this logical color as default for its marked icons TRgb defaultColor; defaultColor = CEikonEnv::Static()->Color(EColorControlText); AknsUtils::CreateColorIconLC(AknsUtils::SkinInstance(), KAknsIIDQgnIndiMarkedAdd, KAknsIIDQsnIconColors, EAknsCIQsnIconColorsCG13, markBitmap, markBitmapMask, KAvkonBitmapFile, EMbmAvkonQgn_indi_marked_add, EMbmAvkonQgn_indi_marked_add_mask, defaultColor ); CGulIcon* markIcon = CGulIcon::NewL( markBitmap,markBitmapMask ); CleanupStack::Pop( 2 ); // markBitmap, markBitmapMask CleanupStack::PushL( markIcon ); iconArray->AppendL( markIcon ); iListBox->ItemDrawer()->ColumnData()->SetIconArray( iconArray ); // markIcon, iconArray CleanupStack::Pop( 2 ); }
This example shows how to change the mark states of a markable list item.
// Marks the second item in the list iListBox->View()->SelectItemL( 1 ); // Unmarks the first item in the list iListBox->View()->DeselectItem( 0 );
Markable and multiselection lists allow multiple items to be selected,
as well as one item or none at all. An array of the indexes of the selected
items can be requested from these lists. The indexes are returned by the listbox
as an array of type CSelectionIndexArray
, which is essentially
just a typedef of CArrayFix
. The following code example demonstrates
how to retrieve the array of selected items from markable and multiselection
lists:
// Get the selected item indexes an array const CArrayFix<TInt> *selectedIndexes = iListBox ->SelectionIndexes(); // Make sure the array is not null (no items) if ( selectedIndexes != NULL ) { // Loop through the selected item indexes for ( TInt index=0; index < selectedIndexes->Count(); index++ ) { // Get the index of the selected item TInt selectedItemIndex = (*aIndexArray)[index]; // now do something with the index… } }
A setting style listbox is essentially a selection listbox with an added
feature that allows to change the items according to a setting. A listbox
consists of a name and a value. Because of the type of the listbox items,
the CAknSettingStyleListBox
class is used. It is also possible
to create a settings lists using class CAknSettingItemList
.
For details please see Setting Lists API documentation
.
To create a listbox from code, follow these steps:
CAknSettingStyleListBox
class.
EAknListBoxSelectionList
to create a markable
list.
The example listbox is a component of a compound control and it is needed
to implement the method ComponentControl()
and CountComponentControls()
.
class CMyContainer : public CCoeControl { CAknSettingStyleListBox * iListBox; CDesCArrayFlat* iListBoxItems; } void CMyContainer::ConstructL() { … CreateWindowL(); // This is a window owning control // Create listbox iListBox = new (ELeave) CAknSettingStyleListBox(); // markable // Construct listbox iListBox->ConstructL( this, EAknListBoxSelectionList | EAknListBoxLoopScrolling ); iListBox->SetContainerWindowL( *this ); // Set scrollbars iListBox->CreateScrollBarFrameL( ETrue ); iListBox->ScrollBarFrame()->SetScrollBarVisibilityL( CEikScrollBarFrame::EOn, CEikScrollBarFrame::EAuto ); // Create listbox item array iListBoxItems = new (ELeave) CDesCArrayFlat(4); // Create listbox items iListBoxItems->AppendL( _L("\tUpdates\t\tOff") ); iListBoxItems->AppendL( _L("\tLanguage\t\tEnglish") ); iListBoxItems->AppendL( _L("\tSounds\t\tOn") ); // Add items to listbox iListBox->Model()->SetItemTextArray( iListBoxItems ); // Listbox deletes the item array iListBox->Model()->SetOwnershipType( ELbmOwnsItemArray ); iListBox->HandleItemAdditionL( ); // Activate Listbox iListBox->SetRect( Rect() ); iListBox->ActivateL(); … }
New items are added to the listbox by appending a new entry to the listbox
item array. First, the listbox item array is retrieved from the listbox. A
new listbox item is then appended to the existing listbox item array and the
listbox is requested to handle the addition of a new item. The HandleItemAdditionL()
method
handles the listbox redrawing and repositions the selection in a sensible
state.
The following example code adds a new entry to a selection listbox:
// Get listbox item array MDesC16Array* textArray = iListBox->Model()->ItemTextArray(); // Add new item to item array static_cast<CDesC16Array*>( textArray )->AppendL( _L( "\tNewItem\t\t" ) ); // Update listbox iListBox->HandleItemAdditionL();
To remove items from the listbox, you must first get the listbox item array from the listbox. Items are deleted from the item list array by specifying the index of the item to be deleted and the number of items to be deleted. The listbox is requested to handle the item deletion and redraw itself to show the changes.
The following example code removes the second item from a listbox:
// Get listbox item array MDesCArray* textArray = iListBox->Model()->ItemTextArray(); // Delete second item from item array static_cast<CDesCArray*>( textArray )->Delete( 1 ); // Handle deletion iListBox->HandleItemRemovalL(); iListBox->DrawNow();
Multiple contiguous items can also be deleted by specifying the count of
the contiguous items to be deleted as the second parameter of the Delete()
method.
In the example above, only the selected item is deleted by specifying the
count as one (1
).
This example shows how to get the number of listbox items.
TInt listboxItems = iListBox->Model()->NumberOfItems();
If the listbox is a component of a compound control, it is needed to inform
the listbox about key events. It is simply forwarding the event to the listbox
from the components OfferKeyEventL()
method.
TKeyResponse CMyContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType) { return iListBox->OfferKeyEventL( aKeyEvent, aType ); }
To observe events if a listbox item is selected, a listbox observer is
needed. The observer class must be inherited from class MEikListBoxObserver
and
must implement the method HandleListBoxEventL()
.
Listbox can set an observer to which it can send other events, usually
from a component to its container control. The control observer interface
is MCoeControlObserver
. For example the EEventStateChanged
event
can be used by a control to report that some piece of internal data has changed,
and hence that any observer should be notified accordingly.
If a custom listbox is created or it is needed to handle situations where
the theme is changed and listbox needs to display different icons, HandleResourceChange()
must
be implemented.
In this example, the class CMyContainer
observes if a
listbox item is selected, by pressing the Enter key.
class CMyContainer : public CCoeControl, MEikListBoxObserver { … /** * From MEikListBoxObserver */ void HandleListBoxEventL( CEikListBox* aListBox, TListBoxEvent aEventType ); CAknColumnListBox* iListBox; … }; void CMyContainer::HandleListBoxEventL( CEikListBox* /*aListBox*/, TListBoxEvent aEventType ) { if ( aEventType == EEventEnterKeyPressed ) { switch ( iListBox->CurrentItemIndex() ) { case 1: // Handle keypress event here… break; default: // Do default handling here… break; } } }
If the current skin supports animated highlight, it is only visible on
the list if the focus is set to list. This example shows how to set the focus
on the list. iListBox
can be a pointer to any type of AVKON
list. For example: CAknSingleGraphicStyleListBox
.
void MyContainer::FocusChanged( TDrawNow aDrawNow ) { CCoeControl::FocusChanged( aDrawNow ); if( iListBox ) { iListBox->SetFocus( IsFocused(), aDrawNow ); } }
This section describes what are the common problems when using a listbox.
ConstructL()
or
in resource file.
LISTBOX
resource
inside of the dialogs needs to be correct.
Listbox uses standard Symbian OS error reporting mechanism. Possible panic circumstances and panic codes are indicated in class or method descriptions.
Leaves and system wide error codes as function return values are used if the error is recoverable. A client application can handle these errors similarly as a normal Symbian platform application.
Exception handling is not needed, except when setting up a layout in the SizeChanged()
method,
which needs to allocate memory.
The amount of reserved memory for listbox depend on the application, but despite the application the amount of reserved memory is relatively small.
None.