You Are Here:

Community: Wiki

This page was last modified on 27 September 2009, at 13:13.

List box example

From Forum Nokia Wiki

Reviewer Approved   

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

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fE25E455E25A6E2582E25E454E25BE44E2595E25E455E259CE25A8E25E456E2594E25B6E25E454E25BBE25B6E25E457E25AE45E25B1E25E455E2588E259BE25E455E25BBE25BAE25E457E259FE25AE44E25E454E25BFE25A1X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZseriesE5f60Q qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZuserE5ftagQSxs60X qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ