Join Now
Quality Rating:
  • Currently 1.0 / 5
(1.0 / 5 - 1 vote cast)
Expertise Level:
  • Currently 1.0 / 5
(1.0 / 5 - 1 vote cast)

This page was last modified 08:43, 20 November 2007.

List box example

From Forum Nokia Wiki

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.

Listbox_Container.cpp

CListboxContainer::CListboxContainer(void)
{		
}
 
CListboxContainer::~CListboxContainer()
{  	
    delete iMyListBox;
}
 
void CListboxContainer::ConstructL()
{
	CreateWindowL();	
	SetRect(CEikonEnv::Static()->EikAppUi()->ClientRect());
		
	ActivateL();
	SetMenuL();
	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;
 
	iHasGrid = EFalse;
	
    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);
    
    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);
	
	// Append Text string to MyArray in here as "1\tMytext",
	// where 1 is the zero based icon index
	// also remember to load and add icons in here... listbox will
	// automatically resize icons
	// thus no need to set any size for them.
	
	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;
}

Listbox_Container.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 Discussions
Thread Thread Starter Forum Replies Last Post
sockets example UI editing s3ni Symbian Networking & Messaging 2 2003-08-26 11:01
list item too long to fit...... harish13_ks General Symbian C++ 7 2007-06-06 09:26
Background image for a list box ggomeze General Symbian C++ 4 2006-04-27 19:42
modify edit box item in a setting list... lotb Symbian User Interface 0 2003-12-17 14:26
MTM: custom menu items in Message list view alphaMail Symbian Networking & Messaging 1 2007-02-07 09:32
 
Powered by MediaWiki