Print Page | Close Window

CXTTreeBase::FindItem method usage

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: Toolkit Pro
Forum Description: Topics Related to Codejock Toolkit Pro
URL: http://forum.codejock.com/forum_posts.asp?TID=9950
Printed Date: 16 July 2025 at 4:14pm
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: CXTTreeBase::FindItem method usage
Posted By: MacW
Subject: CXTTreeBase::FindItem method usage
Date Posted: 24 March 2008 at 7:07am
This method can be used to find text in a CXTTreeBase control or derived class. But the method has a major flaw in my opinion, or I don't understand how to use it.

When you call FindItem for the first time, it will return the HTREEITEM if the item found (if any). An application then usually selects this item to show the user the found item.

If you now want to implement the usual "Find Next" command, you're at a loss. The FindItem method can either take a 0 as the "start item" to search, or the HTREEITEM of the item you want to start your search. If you use 0, and the currently selected item is already a match (as in our case), the FindItem method will just return the same HTREEITEM. It will not search for the "next" match itself.

In order to implement a "find next" command, we will have to identify the "next" item, and then call FindItem with the HTREEITEM of this "next" item.

Identifying the "next" item in a tree control is not trivial, because it involves going up and down the hierarchy as needed (children, siblings, recursive up the parent hierarchy until a not yet visited item is found. Since we don't have the information which items have already been searched by the FindItem method, it will be much better if Codejock would implement the code internally, within the FindItem method.








Replies:
Posted By: MacW
Date Posted: 24 March 2008 at 7:17am
As a work-around:

You can implement the functionality using the GetNextItem and GetPrevItem methods of the CXTTreeBase class, by keeping track of the "last found" item somewhere else, and then using these methods as appropriate.


Posted By: Oleg
Date Posted: 24 March 2008 at 8:02am
Hi,
Its not "work-around" it is designed way for such operation :-)


-------------
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS


Posted By: MacW
Date Posted: 24 March 2008 at 11:36am
Hi,

I have a reproducible bug where calling FindItem with bDownDir = FALSE causes an endless loop within the FindItem method. The search never terminates and I need to force my application to close.

My tree contains one node with the search text. I select this entry, and then call FindItem with the direction upwards. FindItem never finds the entry, but instead falls into a loop.



Posted By: Oleg
Date Posted: 24 March 2008 at 2:14pm
Hello,
 
Yes, seems there is bug if  there are 2 or more root items. :(
 
Do you have one root item ?


-------------
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS


Posted By: mgampi
Date Posted: 25 March 2008 at 4:23am
Hi;
I use this part of code if I want to search for items in a tree. It works very well and I don't have troubles with find and or find next.
In my case I use find/find next with the same toolbar button:
 
void CPVBar::OnSearch(NMHDR* pNMHDR, LRESULT* pResult)
{
  CString strFind = pMyControl_->GetEditText();
  HTREEITEM hItem=0;
  if ((hItem=MyTree_.GetSelectedItem())==0)
   hItem=MyTree_.GetRootItem();
  else
   hItem=MyTree_.GetNextItem(hItem);
  if ((hItem=MyTree_.FindItem(strFind, FALSE, TRUE, FALSE, hItem))!=0)
  {
   VERIFY(MyTree_.SelectItem(hItem));
   MyTree_.EnsureVisible(hItem);
   MyTree_.SetItemBold(hItem);
  }
  *pResult = 1; // Handled;
 }
}


-------------
Martin

Product: Xtreme Toolkit v 22.1.0, new Projects v 24.0.0
Platform: Windows 10 v 22H2 (64bit)
Language: VC++ 2022


Posted By: MacW
Date Posted: 25 March 2008 at 12:05pm
Yes, in this case. The contents of the tree are flexible, and there may be several root items.


Posted By: MacW
Date Posted: 25 March 2008 at 12:17pm
Thanks for helping out 

Unfortunately, it seems that your method does not find all possible matches when a sub-item is selected, and the sibling item above also has a sub-item which would cause a match, like in the attached screen shot.
I start with the orange node, searching upwards for the sub-term "ba".

.


Posted By: mgampi
Date Posted: 25 March 2008 at 4:02pm
Hi;
In my case I only needed forward only search capabilities, but I think it's a good starting point for your requirements. Just call GetPrevItem() if a search with the start item got by GetNextItem() gives no result; voila!


-------------
Martin

Product: Xtreme Toolkit v 22.1.0, new Projects v 24.0.0
Platform: Windows 10 v 22H2 (64bit)
Language: VC++ 2022


Posted By: Oleg
Date Posted: 26 March 2008 at 12:58am
Him
 
Please replace GetLastItem method as
 

HTREEITEM CXTTreeBase::GetLastItem(HTREEITEM hItem) const
{
 // Temporary used variable
 HTREEITEM htiNext;
 // Get the last item at the top level
 if (hItem == NULL)
 {
  hItem = m_pTreeCtrl->GetRootItem();
  htiNext = m_pTreeCtrl->GetNextSiblingItem(hItem);
  while (htiNext != NULL)
  {
   hItem = htiNext;
   htiNext = m_pTreeCtrl->GetNextSiblingItem(htiNext);
  }
 }
 while (m_pTreeCtrl->ItemHasChildren(hItem) != NULL)
 {
  // Find the last child of hItem
  htiNext = m_pTreeCtrl->GetChildItem(hItem);
  while (htiNext != NULL)
  {
   hItem = htiNext;
   htiNext = m_pTreeCtrl->GetNextSiblingItem(htiNext);
  }
 }
 return hItem;
}


-------------
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS


Posted By: mgampi
Date Posted: 26 March 2008 at 4:02am
Hi Oleg;
 
will this fix also be applied in 12.0 final?


-------------
Martin

Product: Xtreme Toolkit v 22.1.0, new Projects v 24.0.0
Platform: Windows 10 v 22H2 (64bit)
Language: VC++ 2022


Posted By: Oleg
Date Posted: 26 March 2008 at 5:14am
Hi,
 
Yes.


-------------
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS


Posted By: MacW
Date Posted: 26 March 2008 at 4:49pm
Hi, Oleg

this fix seems to have solved the problem. I made some initial tests and it looks good



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net