Namespaces
Variants
Actions

List box example in Symbian C++

Jump to: navigation, search
Article Metadata

Article
Created: symbianyucca (04 Apr 2007)
Last edited: hamishwillee (08 May 2013)

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

This page was last modified on 8 May 2013, at 03:02.
226 page views in the last 30 days.
Nokia Developer aims to help you create apps and publish them so you can connect with users around the world.

京ICP备05048969号  © Copyright Nokia 2013 All rights reserved