List box example in Symbian C++
Article Metadata
The CListboxContainer illustrates how to use list boxes in your application. The main function where the list box is made is named MakeListBoxL(). Inside it the list box is constructed as CAknSingleLargeStyleListBox. If you want to change it to something else, you can one of the following S60 list box types:
- CAknSingleStyleListBox
- CAknSingleNumberStyleListBox
- CAknSingleHeadingStyleListBox,
- CAknSingleGraphicStyleListBox
- CAknSingleGraphicHeadingStyleListBox
- CAknSingleNumberHeadingStyleListBox
- CAknSingleLargeStyleListBox
- CAknDoubleStyleListBox
- CAknDoubleNumberStyleListBox
- CAknDoubleLargeStyleListBox
- CAknDoubleGraphicStyleListBox
- CAknDoubleTimeStyleListBox
In GetArrayL() you need to construct the text strings for the list box according to the selected list box type. When using S60 specific list boxes, the icon size will be automatically set by the list box, so you don’t need to set any size for the icons.
Remember to add CListboxContainer instance to control stack (with AddToStackL()) after creation to get notifications of key events in OfferKeyEventL().
CListboxContainer.cpp
#include "CListboxContainer.h"
#include <akniconarray.h>
#include <eikclbd.h>
CListboxContainer::CListboxContainer(void)
{
}
CListboxContainer::~CListboxContainer()
{
delete iMyListBox;
}
void CListboxContainer::ConstructL()
{
CreateWindowL();
SetRect(iEikonEnv->EikAppUi()->ClientRect());
ActivateL();
DrawNow();
}
void CListboxContainer::SizeChanged()
{
TRAP_IGNORE( MakeListBoxL() );
}
void CListboxContainer::HandleResourceChange(TInt aType)
{
TRect rect;
if ( aType==KEikDynamicLayoutVariantSwitch )
{
AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
SetRect(rect);
}
CCoeControl::HandleResourceChange(aType);
}
void CListboxContainer::MakeListBoxL()
{
TInt MySetIndex(0);
if(iMyListBox)
{
MySetIndex = iMyListBox->CurrentItemIndex();
}
delete iMyListBox;
iMyListBox = NULL;
iMyListBox = new( ELeave ) CAknSingleLargeStyleListBox();
iMyListBox->ConstructL(this,EAknListBoxSelectionList);
CArrayPtr<CGulIcon>* icons =new( ELeave ) CAknIconArray(1);
CleanupStack::PushL(icons);
iMyListBox->Model()->SetItemTextArray(GetArrayL(icons));
iMyListBox->Model()->SetOwnershipType(ELbmOwnsItemArray);
//
// Inform the listbox of item additions.
// This should be done if you want your items displayed correctly
iMyListBox ->HandleItemAdditionL();
CleanupStack::Pop(icons);
iMyListBox->ItemDrawer()->ColumnData()->SetIconArray(icons);
iMyListBox->CreateScrollBarFrameL( ETrue );
iMyListBox->ScrollBarFrame()->SetScrollBarVisibilityL(
CEikScrollBarFrame::EOff, CEikScrollBarFrame::EAuto);
iMyListBox->SetRect(Rect());
iMyListBox->View()->SetListEmptyTextL(KtxNoData);
iMyListBox->ActivateL();
TInt ItemsCount = iMyListBox->Model()->ItemTextArray()->MdcaCount();
if(ItemsCount > MySetIndex && MySetIndex >= 0)
iMyListBox->SetCurrentItemIndex(MySetIndex);
else if(ItemsCount > 0)
iMyListBox->SetCurrentItemIndex(0);
UpdateScrollBar(iMyListBox);
iMyListBox->DrawNow();
}
CDesCArray* CListboxContainer::GetArrayL(CArrayPtr<CGulIcon>* aIcon)
{
CDesCArrayFlat* myArray = new(ELeave)CDesCArrayFlat(1);
CleanupStack::PushL(myArray);
// 1) Append Text string to myArray in here as "1\tMytext",
// where 1 is the zero based icon index
//
// Second and third icon indexes can also be specified in the form:
// "1\tMytext\t2\t3" where 2 and 3 are also zero-based icon indexes
//
// 2) Remember to load and add icons in here. Listbox will
// automatically resize iconsthus no need to set any size for them.
// Tip: you may find AknIconUtils::CreateIconLC and
// AknIconUtils::AvkonIconFileName() useful. In that case,
// include akniconutils.h and avkon.mbg
CleanupStack::Pop(myArray);
return myArray;
}
void CListboxContainer::UpdateScrollBar(CEikListBox* aListBox)
{
if (aListBox)
{
TInt pos(aListBox->View()->CurrentItemIndex());
if (aListBox->ScrollBarFrame())
{
aListBox->ScrollBarFrame()->MoveVertThumbTo(pos);
}
}
}
void CListboxContainer::Draw(const TRect& /*aRect*/) const
{
CWindowGc& gc = SystemGc();
gc.Clear(Rect());
}
TKeyResponse CListboxContainer::OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aEventCode)
{
TKeyResponse Ret = EKeyWasNotConsumed;
if(iMyListBox)
{
Ret = iMyListBox->OfferKeyEventL(aKeyEvent,aEventCode);
}
return Ret;
}
CCoeControl* CListboxContainer::ComponentControl( TInt /*aIndex*/) const
{
return iMyListBox;
}
TInt CListboxContainer::CountComponentControls() const
{
if(iMyListBox)
return 1;
else
return 0;
}
CListboxContainer.h
#include <aknlists.h>
#include <gulicon.h>
_LIT(KtxNoData, "Empty list");
class CListboxContainer : public CCoeControl
{
public:
CListboxContainer(void);
void ConstructL(void);
~CListboxContainer();
public:
TKeyResponse OfferKeyEventL(const TKeyEvent& aKeyEvent,TEventCode aType);
CCoeControl* ComponentControl( TInt aIndex) const;
TInt CountComponentControls() const;
private:
virtual void SizeChanged();
virtual void HandleResourceChange(TInt aType);
void Draw(const TRect& aRect) const;
CDesCArray* GetArrayL(CArrayPtr<CGulIcon>* aIcon);
void MakeListBoxL(void);
void UpdateScrollBar(CEikListBox* aListBox);
private:
CAknSingleLargeStyleListBox* iMyListBox;
};
Related Links
- How to create a simple listbox
- Querying selection with list
- TSS000647 - Applying actions on list box items
- How to add Marquee effect in ListBox
- How to add ScrollBar in ListBox
- How to add additional data to the list box?
- Listbox creation using resource
- How to scroll a customized list box without using a scroll bar


I don't think it makes sense to delete and re-create the listbox every time the framework calls SizeChanged() on the container. It should be enough to create the listbox once in the ConstructL() and then call DrawDeferred() in SizeChanged(). There was also a call to a leaving function in SizeChanged() which is non-leaving. I added a TRAP_IGNORE just to cover it up, but the whole thing should be fixed.
Also calling DrawNow() at the end of ConstructL() is not a good idea. Window server will draw the the container in due time, you should not force it. You should try to avoid ever calling DrawNow() unless it is really necessary.
This is not a very good example of how listboxes should be used.
- Marko