Page 1 of 1

Iterating a ITreeViewItems collection

Posted: 18 Jan 2006, 07:51
by Guest
Hi Timo,

I have a selection of items in a ITreeViewItems class. I would like to iterate over this selection using a for loop until Count(). I see you have an Items() function that should do the job, but I do not know what to pass as parameter for the itemIdentifierType. I tried iitID, that does not seem to work and iitHandle expects a valid item handle....

Kind regards,

Chris

Posted: 18 Jan 2006, 13:16
by TiKu
In VB you use a For-Each loop:

Code: Select all

Dim ti As TreeViewItem

For Each ti In ExTvw.TreeItems
  ' do something with ti
Next ti
C++ doesn't have such a construct, but you can do what For Each is doing behind the scences:

Code: Select all

// pTvwItems is a ITreeViewItems*
IEnumVARIANT* pEnumerator;
pTvwItems->QueryInterface(IID_IEnumVARIANT, reinterpret_cast<void**>(&pEnumerator));
if(pEnumerator) {
	VARIANT item;
	VariantInit(&item);
	ULONG dummy = 0;
	while(pEnumerator->Next(1, &item, &dummy) == S_OK) {
		if((item.vt == VT_DISPATCH) && item.pdispVal) {
			ITreeViewItem* pTvwItem;
			item.pdispVal->QueryInterface(IID_ITreeViewItem, reinterpret_cast<void**>(&pTvwItem));
			if(pTvwItem) {
				// do something with pTvwItem
				pTvwItem->Release();
			}
		}
		VariantClear(&item);
	}
	pEnumerator->Release();
}
HTH
TiKu