Profile Picture

How to apply Filter in NTreeViewEx

Posted By Tobias Breuer 9 Years Ago
Author
Message
Tobias Breuer
Question Posted 9 Years Ago
View Quick Profile
Forum Newbie

Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: Forum Members
Last Active: 9 Years Ago
Posts: 5, Visits: 11
Hi, I'm currently struggling with filtering an NTreeViewEx based on the Name / Text of the NTreeNodes contained in the TreeView. What I'm currently doing is: I have a TextBox where I listen for text changes (buffered to update only every 500 ms). When the buffering time is elapsed, I want to apply the filter and list only those nodes, that contain the given filter text.

To achieve this, I create a new NTreeNodeTextFilter and set this to the VisibleFilter property of the NTreeViewEx. The options are CommonStringOptions.Contains and case match is set to false.
Sometimes, this filter seems to do something (e.g. when I enter 't' it lists some nodes that actually contain a t but not all - when I extend the filter to 'ti' it does not find anything even though there are nodes with this text. Setting it back to 't' now also does not list any items any more. Not even those he found before).

I actually tried a lot of different approaches without any luck:
- Create a new NTreeNodeTextFilter every time the search text changed
- keep the same instance of NTreeNodeTextFilter and only update its Text property
- calling ForceUpdate on the TreeViewEx after applying the filter
- using NTreeNodeNameFilter instead.
- applying the filter to the Nodes collection of the NTreeViewEx -> this will result in no items showing up ever

Btw: I'm using Nevron version 13.5.15
Can you please provide a basic example on how to Filter the NTreeViewEx to show only those nodes that in their Text contain a given search string?

Thanks in advance and best regards,

Tobias

Nevron Support
Posted 9 Years Ago
View Quick Profile
Supreme Being

Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)

Group: Forum Members
Last Active: Last Year
Posts: 3,039, Visits: 3,746
Hello Tobias,

Try to place the following code into the textbox TextChanged event handler:


string text = nTextBox1.Text;

NTreeNodeTextFilter filter = new NTreeNodeTextFilter();
filter.CaseSensitive = false;
filter.Options = CommonStringOptions.Contains;
filter.Text = text;

if (string.IsNullOrEmpty(text))
{
    nTreeViewEx1.VisibleFilter = null;
}
else
{
    nTreeViewEx1.VisibleFilter = filter;
}


I hope this helps.


Best Regards,
Nevron Support Team



Tobias Breuer
Posted 9 Years Ago
View Quick Profile
Forum Newbie

Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: Forum Members
Last Active: 9 Years Ago
Posts: 5, Visits: 11
Hi,

unfortunately this is something I already tried (meant with the point "- Create a new NTreeNodeTextFilter every time the search text changed")
But I think I now found out my missunderstanding. So you could maybe enlighten me:

Let's assume I have the following hierarchy within the Tree:
  • ParentNodeName
    • SomeSubNode
      • SubSubNodeA
      • SubSubNodeB
    • AnotherSubNode
      • AnotherSubSubNodeA
      • AnotherSubSubNodeB
When I now enter the search text "SubNodeB" with the filter being set to option "Contains" I would expect the tree view to list
  • SubSubNodeB
  • AnotherSubNodeB
And when I enter "SubNode" I would expect the view to show all the nodes except for "ParentNodeName".

But it seems to be the case, that these filters will not show up anything because the parent already does not contain "Sub" and so the result set in both cases is empty.

How can I use the filter to allow for the above mentioned case i.e. list all nodes containing the given filter text, regardless of their hierarchy level and their parents names?

Thanks in advance and best regards,

Tobias

Nevron Support
Posted 9 Years Ago
View Quick Profile
Supreme Being

Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)

Group: Forum Members
Last Active: Last Year
Posts: 3,039, Visits: 3,746
Hi Tobias,

Here is an example implementation of the required functionality:

First cache the original nodes so when the filter is empty to set back the NTreeViewEx nodes with them:

NTreeNodeCollection m_CachedNodes;
...
//Cache the initial nodes of NTreeViewEx
m_CachedNodes = new NTreeNodeCollection();
for (int i = 0; i < nTreeViewEx1.Nodes.Count; i++)
{
    NTreeNode node = nTreeViewEx1.Nodes[i];
    node.CloneChildren = true;
    m_CachedNodes.Add((NTreeNode)node.Clone());
}


Use the following code when the text of the filter is changed:

string text = nTextBox1.Text;

NTreeNodeTextFilter filter = new NTreeNodeTextFilter();
filter.CaseSensitive = false;
filter.Options = CommonStringOptions.Contains;
filter.Text = text;

if (string.IsNullOrEmpty(text))
{
    //When the filter is empty set the NTreeViewEx with the original nodes.
    nTreeViewEx1.Nodes.Clear();
    nTreeViewEx1.VisibleFilter = null;

    for (int i = 0; i < m_CachedNodes.Count; i++)
    {
        NTreeNode node = m_CachedNodes[i];
        nTreeViewEx1.Nodes.Add(node);
    }
}
else
{
    //Apply the filter on NTreeViewEx control, and set the nodes which are not
    //hidden from the filter to its Nodes collection.
    nTreeViewEx1.Nodes.Clear();
    nTreeViewEx1.VisibleFilter = filter;

    List<NTreeNode> nodes = GetVisibleNodes(m_CachedNodes);
    for (int i = 0; i < nodes.Count; i++)
    {
        NTreeNode node = nodes[i];
        node.CloneChildren = false;
        nTreeViewEx1.Nodes.Add((NTreeNode)node.Clone());
    }
}

...

private List<NTreeNode> GetVisibleNodes(NTreeNodeCollection nodes)
{
    List<NTreeNode> result = new List<NTreeNode>();
    for (int i = 0; i < nodes.Count; i++)
    {
        NTreeNode node = nodes[i];
        if (!node.IsHiddenByFilter)
        {
            result.Add(node);
        }
  
        NTreeNodeCollection subNodes = node.Nodes as NTreeNodeCollection;
        if (subNodes != null)
        {
            result.AddRange(GetVisibleNodes(subNodes));
        }
    }
    return result;
}


This way you will have all nodes that are not filtered as a list in NTreeViewEx control.

In the next release we will modify the NTreeNodeStringFilter so it should be able to filter nodes in more advance way.

I hope this helps.

Best Regards,
Nevron Support Team





Similar Topics


Reading This Topic