Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > Toolkit Pro
  New Posts New Posts RSS Feed - CXTTreeBase::FindItem  method usage
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

CXTTreeBase::FindItem method usage

 Post Reply Post Reply
Author
Message
MacW View Drop Down
Senior Member
Senior Member


Joined: 26 June 2007
Status: Offline
Points: 253
Post Options Post Options   Thanks (0) Thanks(0)   Quote MacW Quote  Post ReplyReply Direct Link To This Post Topic: CXTTreeBase::FindItem method usage
    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.





Back to Top
MacW View Drop Down
Senior Member
Senior Member


Joined: 26 June 2007
Status: Offline
Points: 253
Post Options Post Options   Thanks (0) Thanks(0)   Quote MacW Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
MacW View Drop Down
Senior Member
Senior Member


Joined: 26 June 2007
Status: Offline
Points: 253
Post Options Post Options   Thanks (0) Thanks(0)   Quote MacW Quote  Post ReplyReply Direct Link To This Post 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.

Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
mgampi View Drop Down
Senior Member
Senior Member
Avatar

Joined: 14 July 2003
Status: Offline
Points: 1198
Post Options Post Options   Thanks (0) Thanks(0)   Quote mgampi Quote  Post ReplyReply Direct Link To This Post 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 19.0.0, new Projects v 19.1.0
Platform: Windows 10 v 1909 (64bit)
Language: VC++ 2017
Back to Top
MacW View Drop Down
Senior Member
Senior Member


Joined: 26 June 2007
Status: Offline
Points: 253
Post Options Post Options   Thanks (0) Thanks(0)   Quote MacW Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
MacW View Drop Down
Senior Member
Senior Member


Joined: 26 June 2007
Status: Offline
Points: 253
Post Options Post Options   Thanks (0) Thanks(0)   Quote MacW Quote  Post ReplyReply Direct Link To This Post 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".

.
Back to Top
mgampi View Drop Down
Senior Member
Senior Member
Avatar

Joined: 14 July 2003
Status: Offline
Points: 1198
Post Options Post Options   Thanks (0) Thanks(0)   Quote mgampi Quote  Post ReplyReply Direct Link To This Post 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 19.0.0, new Projects v 19.1.0
Platform: Windows 10 v 1909 (64bit)
Language: VC++ 2017
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
mgampi View Drop Down
Senior Member
Senior Member
Avatar

Joined: 14 July 2003
Status: Offline
Points: 1198
Post Options Post Options   Thanks (0) Thanks(0)   Quote mgampi Quote  Post ReplyReply Direct Link To This Post Posted: 26 March 2008 at 4:02am
Hi Oleg;
 
will this fix also be applied in 12.0 final?
Martin

Product: Xtreme Toolkit v 19.0.0, new Projects v 19.1.0
Platform: Windows 10 v 1909 (64bit)
Language: VC++ 2017
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post Posted: 26 March 2008 at 5:14am
Hi,
 
Yes.
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS
Back to Top
MacW View Drop Down
Senior Member
Senior Member


Joined: 26 June 2007
Status: Offline
Points: 253
Post Options Post Options   Thanks (0) Thanks(0)   Quote MacW Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.172 seconds.