how to create list box example

来源:互联网 发布:送闺蜜新年礼物 知乎 编辑:程序博客网 时间:2024/05/16 11:10

 

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 stringsfor the list box according to the selected list box type. When usingS60 specific list boxes, the icon size will be automatically set by thelist box, so you don’t need to set any size for the icons.Remember to add CListboxContainer instance to control stack (withAddToStackL()) after creation to get notifications of key events inOfferKeyEventL().

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
原创粉丝点击