http://library.forum.nokia.com/index.jsp?topic=/S60_3rd_Edition_Cpp_Developers_Library/GUID-E3DA1ADE-3380-44D3-ACD0-91B0C8626AAD.html
Creating a multiselection listbox
In a multiselection list, different list items can be selected at the same time, to perform some operation on them. A listbox consists of a checkbox icon, and a text. Because of the type of the listbox items, the CAknSingleGraphicStyleListBox
class is used, so a checkbox can be used on the left side on the list item. To create a listbox from code, follow these steps:
- Create a new listbox instance. You must choose an appropriate class to instantiate depending on which type of listbox you wish to create This example uses the
CAknSingleGraphicStyleListBox
class. - Use flag
EAknListBoxMultiselectionList
to create a multiselection list. - Set the listbox’s container window. Since the listbox is a non window-owning control, its parent control is passed as a parameter, providing a window for the listbox to draw to.
- Create a scrollbar frame to the listbox and set active.
- Construct a listbox item array. The listbox item string format must obey the string format specified for the listbox type used. Append the formatted item strings to the listbox item array.
- Set the listbox item array to the listbox and set the item array ownership to the listbox.
- Construct the list icon array, that contains the checkbox icons The checkbox icons are loaded from a AVKON's bitmap file and appended to the icon array. Set the icon array to the listbox. In the example the checkbox icons are gotten from the current active skin, so if the skin changes, the checkbox icons change as well, according to the new skin.
Note that the order of the icons in the icon array defines their indexes. In this example, the first icon is indexed as zero (0), and the next is indexed as one (1). The first column in the listbox item string defines the index of the icon to be displayed for the specific listbox item.
The example listbox is a component of a compound control and it is needed to implement the method ComponentControl()
and CountComponentControls()
.
class CMyContainer : public CCoeControl { CAknColumnListBox* iListBox; CDesCArrayFlat* iListBoxItems; } void CMyContainer::ConstructL() { … CreateWindowL(); // This is a window owning control // Create listbox iListBox = new (ELeave) CAknSingleGraphicStyleListBox(); // markable // Construct listbox iListBox->ConstructL( this, EAknListBoxMultiselectionList| EAknListBoxLoopScrolling ); iListBox->SetContainerWindowL( *this ); // Set scrollbars iListBox->CreateScrollBarFrameL( ETrue ); iListBox->ScrollBarFrame()->SetScrollBarVisibilityL( CEikScrollBarFrame::EOn, CEikScrollBarFrame::EAuto ); // Create listbox item array iListBoxItems = new (ELeave) CDesCArrayFlat(4); // Create listbox items iListBoxItems->AppendL( _L("1/tItem1") ); iListBoxItems->AppendL( _L("1/tItem2") ); iListBoxItems->AppendL( _L("1/tItem3") ); // Add items to listbox iListBox->Model()->SetItemTextArray( iListBoxItems ); // Listbox deletes the item array iListBox->Model()->SetOwnershipType( ELbmOwnsItemArray ); iListBox->HandleItemAdditionL( ); // Add icons to listbox AddCheckboxIconsL(); // Activate Listbox iListBox->SetRect( Rect() ); iListBox->ActivateL(); … } void CMyContainer::AddCheckboxIconsL() { CAknIconArray* iconArray = new( ELeave ) CAknIconArray( 1 ); CleanupStack::PushL( iconArray ); CFbsBitmap* checkboxOnBitmap = NULL; CFbsBitmap* checkboxOnBitmapMask = NULL; CFbsBitmap* checkboxOffBitmap = NULL; CFbsBitmap* checkboxOffBitmapMask = NULL; //CListItemDrawer is using this logical color as default for its marked icons TRgb defaultColor; defaultColor = CEikonEnv::Static()->Color( EColorControlText ); AknsUtils::CreateColorIconLC( AknsUtils::SkinInstance(), KAknsIIDQgnPropCheckboxOff, KAknsIIDQsnIconColors, EAknsCIQsnIconColorsCG13, checkboxOnBitmap, checkboxOnBitmapMask, KAvkonBitmapFile, EMbmAvkonQgn_indi_checkbox_on, EMbmAvkonQgn_indi_checkbox_on_mask, defaultColor ); CGulIcon* checkboxOnIcon = CGulIcon::NewL( checkboxOnBitmap, checkboxOnBitmapMask ); CleanupStack::Pop( 2 ); // checkboxOnBitmap, checkboxOnBitmapMask CleanupStack::PushL( checkboxOnIcon ); iconArray->AppendL( checkboxOnIcon ); AknsUtils::CreateColorIconLC( AknsUtils::SkinInstance(), KAknsIIDQgnPropCheckboxOff, KAknsIIDQsnIconColors, EAknsCIQsnIconColorsCG13, checkboxOffBitmap, checkboxOffBitmapMask, KAvkonBitmapFile, EMbmAvkonQgn_indi_checkbox_off, EMbmAvkonQgn_indi_checkbox_off_mask, defaultColor ); CGulIcon* checkboxOffIcon = CGulIcon::NewL( checkboxOffBitmap, checkboxOffBitmapMask ); CleanupStack::Pop( 2 ); // checkboxOffBitmap, checkboxOffBitmapMask CleanupStack::PushL( checkboxOffIcon ); iconArray->AppendL( checkboxOffIcon ); iListBox->ItemDrawer()->ColumnData()->SetIconArray( iconArray ); // checkboxOffIcon, checkboxOnIcon, iconArray CleanupStack::Pop( 3 ); }
Select and deselect an item in a multiselection listbox
This example shows how to change the checkbox states of a multiselection list item.
// Checks the second item in the list iListBox->View()->SelectItemL( 1 ); // Unchecks the first item in the list iListBox->View()->DeselectItem( 0 );
Creating a markable listbox
A markable listbox is essentially a selection listbox with an added feature that allows list items to be marked. Any number of items can be marked on the list. A command can be applied to all of the marked items. A listbox consists of a mark icon, if the item is marked and a text. Because of the type of the listbox items, the CAknSingleGraphicStyleListBox
class is used, so a mark icon can be used on the right side on the list item. To create a listbox from code, follow these steps:
- Create a new listbox instance. You must choose an appropriate class to instantiate depending on which type of listbox you wish to create This example uses the
CAknSingleGraphicStyleListBox
class. - Use flag
EAknListBoxMarkableList
to create a markable list. - Set the listbox’s container window. Since the listbox is a non window-owning control, its parent control is passed as a parameter, providing a window for the listbox to draw to.
- Create a scrollbar frame to the listbox and set active.
- Construct a listbox item array. The listbox item string format must obey the string format specified for the listbox type used. Append the formatted item strings to the listbox item array.
- Set the listbox item array to the listbox and set the item array ownership to the listbox.
- Construct the list icon array, that contains the mark icons The mark icons are loaded from a AVKON's bitmap file and appended to the icon array. Set the icon array to the listbox. In the example the mark icons are gotten from the current active skin, so if the skin changes, the mark icons change as well, according to the new skin.
Note that the order of the icons in the icon array defines their indexes. In this example, the first icon is indexed as zero (0
), and the next is indexed as one (1
). The first column in the listbox item string defines the index of the icon to be displayed for the specific listbox item.
The example listbox is a component of a compound control and it is needed to implement the method ComponentControl()
and CountComponentControls()
.
class CMyContainer : public CCoeControl { CAknColumnListBox* iListBox; CDesCArrayFlat* iListBoxItems; } void CMyContainer::ConstructL() { … CreateWindowL(); // This is a window owning control // Create a single numbered style listbox iListBox = new (ELeave) CAknSingleGraphicStyleListBox(); // markable // Construct listbox iListBox->ConstructL( this, EAknListBoxMarkableList | EAknListBoxLoopScrolling ); iListBox->SetContainerWindowL( *this ); // Set scrollbars iListBox->CreateScrollBarFrameL( ETrue ); iListBox->ScrollBarFrame()->SetScrollBarVisibilityL( CEikScrollBarFrame::EOn, CEikScrollBarFrame::EAuto ); // Create listbox item array iListBoxItems = new (ELeave) CDesCArrayFlat(4); // Create listbox items iListBoxItems->AppendL( _L("/tItem1/t/t") ); iListBoxItems->AppendL( _L("/tItem2/t/t") ); iListBoxItems->AppendL( _L("/tItem3/t/t") ); // Add items to listbox iListBox->Model()->SetItemTextArray( iListBoxItems ); // Listbox deletes the item array iListBox->Model()->SetOwnershipType( ELbmOwnsItemArray ); iListBox->HandleItemAdditionL( ); // Add icons to listbox AddMarkIconsL(); // Activate Listbox iListBox->SetRect( Rect() ); iListBox->ActivateL(); … } void CMyContainer::AddMarkIconsL() { CAknIconArray* iconArray = new(ELeave) CAknIconArray(1); CleanupStack::PushL(iconArray); CFbsBitmap* markBitmap = NULL; CFbsBitmap* markBitmapMask = NULL; //CListItemDrawer is using this logical color as default for its marked icons TRgb defaultColor; defaultColor = CEikonEnv::Static()->Color(EColorControlText); AknsUtils::CreateColorIconLC(AknsUtils::SkinInstance(), KAknsIIDQgnIndiMarkedAdd, KAknsIIDQsnIconColors, EAknsCIQsnIconColorsCG13, markBitmap, markBitmapMask, KAvkonBitmapFile, EMbmAvkonQgn_indi_marked_add, EMbmAvkonQgn_indi_marked_add_mask, defaultColor ); CGulIcon* markIcon = CGulIcon::NewL( markBitmap,markBitmapMask ); CleanupStack::Pop( 2 ); // markBitmap, markBitmapMask CleanupStack::PushL( markIcon ); iconArray->AppendL( markIcon ); iListBox->ItemDrawer()->ColumnData()->SetIconArray( iconArray ); // markIcon, iconArray CleanupStack::Pop( 2 ); }
Mark and unmark an item in a markable listbox
This example shows how to change the mark states of a markable list item.
// Marks the second item in the list iListBox->View()->SelectItemL( 1 ); // Unmarks the first item in the list iListBox->View()->DeselectItem( 0 );
Getting the selected items of a markable or multiselection list
Markable and multiselection lists allow multiple items to be selected, as well as one item or none at all. An array of the indexes of the selected items can be requested from these lists. The indexes are returned by the listbox as an array of type CSelectionIndexArray
, which is essentially just a typedef of CArrayFix
. The following code example demonstrates how to retrieve the array of selected items from markable and multiselection lists:
// Get the selected item indexes an array const CArrayFix<TInt> *selectedIndexes = iListBox ->SelectionIndexes(); // Make sure the array is not null (no items) if ( selectedIndexes != NULL ) { // Loop through the selected item indexes for ( TInt index=0; index < selectedIndexes->Count(); index++ ) { // Get the index of the selected item TInt selectedItemIndex = (*aIndexArray)[index]; // now do something with the index… } }
注意:不能显示可以加上 iListBox->SetExtent(TPoint(0,0) , iListBox->MinimumSize()) ;试试。