ListBox: Part VI
ListBox creation
The following code snippet shows how to create and initialize listbox. Certain parts of initialization are slightly different in case of CEikColumnListBox-derived and CEikFormattedCellListBox-derived listboxes.
ListBox Container Code:
CEikColumnListBox* _listBox = new (ELeave) CEikColumnListBox-derived ListBox;
or
CEikFormattedCellListBox* _listBox = new (ELeave) CEikFormattedCellListBox-derived ListBox;
_listBox->ConstructL(this); // parent: container, use default flags
_listBox->SetContainerWindowL(*this);
_listBox->CreateScrollBarFrameL(ETrue);
_listBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOn, CEikScrollBarFrame::EAuto);
// Setting the icon list
// For CEikColumnListBox-derived ListBox
CArrayPtr* iconList = (method that returns icon list);
_listBox->ItemDrawer()->ColumnData()->SetIconArray(iconList);
or
// For CEikFormattedCellListBox-derived ListBox
_listBox->ItemDrawer()->FormattedCellData()->SetIconArray();
_listBox->ItemDrawer()->FormattedCellData()->SetSubCellAlignmentL(2, CGraphicsContext::ELeft);
// Setting the item list (in this example listbox takes the ownership of the list)
CDesCArray* listBoxItems = (method that returns listbox items);
// Items could be read also from a resource file (ARRAY structure in .rss file),
// in cases where item list is non-modifiable (and perhaps language-dependent)
// CDesCArrayFlat* listBoxItems = iCoeEnv->ReadDesCArrayResourceL(R_MYAPP_LISTBOX_ITEMS);
CTextListBoxModel* model = _listBox->Model();
model->SetItemTextArray(listBoxItems);
model->SetOwnershipType(ELbmOwnsItemArray); // transfers ownership
Icons in a ListBox
The icon list is a multi-bitmap file. The icons used in a listbox can have different sizes: typically, you'll have big and small icons in your icon list. Small icon size could be something like 13x13 pixels, while good candidates for a big icon size are 30x40, 36x44, 40x30, 40x48 and 44x36. (All dimensions should be taken just as a guideline.)
The following code shows how to create icons for a listbox, assuming that icons are stored in a multi-bitmap (.mbm) file located in the application's installation directory.
// CGulIcon class packages two bitmaps: icon image and its mask
// CAknIconArray inherits from CArrayPtrFlat
CArrayPtr* iconList = new (ELeave) CAknIconArray(10);
CleanupStack::PushL(iconList);
// Icons are referenced by their physical position in .mbm file
iconList->AppendL(iEikonEnv->CreateIconL(KIconsFilename, <icon1 idx>, <icon1mask idx>));
iconList->AppendL(iEikonEnv->CreateIconL(KIconsFilename, <icon2 idx>, <icon2mask idx>));
...
CleanupStack::Pop();
// assign icon list to a listbox...
How to add an item
The following code shows how to add an item to the listbox.
MDesCArray* textArray = model->ItemTextArray();
CDesCArray* listBoxItems = static_cast<CDesCArray*>(textArray);
// The actual item text format depends on the listbox type, see tables with listbox types
_LIT(KItemName,"MyNewItem");
TBuf<32> item;
item.Format(_L("%d\t%S\t%d\t%d"), 0, &KItemName, 1, 2); // 0, 1, 2 = icon indexes in iconlist
listBoxItems->AppendL(item);
_listBox->HandleItemAdditionL(); // Update listbox
_listBox->SetCurrentItemIndexAndDraw(listBoxItems->Count() - 1); // select new item
How to remove an item
The following code shows how to remove an item from the listbox.
TInt currentItem = _listBox->CurrentItemIndex();
MDesCArray* textArray = model->ItemTextArray();
CDesCArray* listBoxItems = static_cast<CDesCArray*>(textArray);
listBoxItems->Delete(currentItem, 1); // 1 = how many items to delete
AknListBoxUtils::HandleItemRemovalAndPositionHighlightL(_listBox, currentItem, ETrue);
_listBox->DrawNow(); // Update listbox






> ListBox: Part VI
Can anybody tell me what are the header files invloved in this tutorial??
Tanx
> ListBox: Part VI
> ListBox: Part VI
#include akniconarray.h // CAknIcon #include aknlists.h //CAknSingleStyleListBox #include aknutils.h #include barsread.h // TResource Reader #include eikclbd.h // CColumnListBoxData #include eikmenub.h // CEikMenuBar #include eiktxlbm.h #include eiklbx.h #include eiklbm.h #include eiktxlbx.h
#include <stringloader.h> // StringLoader #include <uikon.hrh> // TKeyCode #defines #include <eiklabel.h> // for example label control #include<badesca.h>
ListBox: Part VI
>> item.Format(_L("%d\t%S\t%d\t%d"), 0, &KItemName, 1, 2); // 0, 1, 2 = icon indexes in iconlist
and if for some item i don't want any icon? what should be the symbol then? just a space or "-" don't work...
ListBox: Part VI
If you want the main item icon to be empty, you'll need to create an icon with no foreground to it and just use its index. If you want the trailing icons to be blank, just exclude them from the item text, such as:
item.Format(_L("%d\t%S"), 0, &KItemName); // 0 = icon index in iconlist
Cheers,
TT
Re: ListBox: Part VI
hi every body
i write a simple code into the multiviews example, i add a method to view's container. the code is follow
void CMultiViewsContainer1::DisplayListBox()
{
m_listBox = new (ELeave) CAknSingleStyleListBox();
m_listBox->SetListBoxObserver(this);
m_listBox->SetContainerWindowL(*this);
m_listBox->ConstructL(this, 0);
(textArray);
m_listBox->CreateScrollBarFrameL(ETrue);
m_listBox->ScrollBarFrame()->SetScrollBarVisibilityL(CEikScrollBarFrame::EOn, CEikScrollBarFrame::EAuto);
CTextListBoxModel* model = m_listBox->Model();
MDesCArray* textArray = model->ItemTextArray();
CDesCArray* listBoxItems = static_cast
model->SetItemTextArray(listBoxItems);
model->SetOwnershipType(ELbmOwnsItemArray);
TBuf<32> item;
_LIT(KIT1, "item 1");
_LIT(KIT2, "item 2");
item.Format(_L("\t%S\t\t"), &KIT1);
listBoxItems->AppendL(item);
return;
item.Format(_L("%S"), &KIT2);
listBoxItems->AppendL(item);
m_listBox->HandleItemAdditionL();
m_listBox->SetCurrentItemIndexAndDraw(listBoxItems->Count() - 1);
m_listBox->ActivateL();
DrawNow();
}
when i execute it in epoc, i got a error of cbase21, if i do not execute appendl of CDesCArray, it run ok , who can tell me why?
thanks
steve