Click here to Skip to main content
13,299,233 members (43,516 online)
Click here to Skip to main content
Add your own
alternative version


38 bookmarked
Posted 10 Apr 2006

TreeView filter

, 10 Apr 2006
Rate this:
Please Sign up or sign in to vote.
A TreeView control that implements node search.

Sample Image


This article illustrates a treeview control that implements node search.

Using the control

The library is very similar to the TreeView library available in Windows Forms. It includes three classes: TreeViewEx, TreeNodeEx, and TreeNodeCollectionEx, and an interface: INodeItemsFilter. All classes behave almost the same as their Windows Forms counterparts.

Implementing a node with filtering/search

In order to implement a search mechanism for your filter node, you need to define a filter class inherited from INodeItemsFilter. Take, for example, the following code:

using Asterix.Controls;
public class FindMyNodes : INodeItemsFilter {
    TreeNodeEx[] GetItems(string query) {
        ArrayList items = new ArrayList(20);

        // Create 20 items and set their text.
        for (int i = 0; i < 20; i++) {
            items.Add(new TreeNodeEx(
                  string.Format("{0}: {1}", query, i))); 

        return (TreeNodeEx[]) items.ToArray(typeof(TreeNodeEx));

Pass an instance of FindMyNodes to the constructor of TreeNodeEx, and set it as FilterNode to one of the nodes in your collection. Take, for example, the following code:

treeViewEx1.Nodes[0].FilterNode = 
     new TreeNodeEx(string.Empty, new FindMyNodes());

Now, the first node in your collection expands to a search form. Type the search query and press Enter. The TreeView will be filled with the nodes that match your query (in this case, 20 items returned by FindMyNodes). That's it.

Points of Interest

The control fully implements visual styles with no overhead, except for the .NET framework 2.0. Still, the project could be compiled on Visual Studio .NET 2003 with little change.

The problem with visual styles, that I found pretty tricky, is that ScrollableControl doesn't implement borders. Without borders implemented by the base class, I couldn't create borders that look like those of a TreeView. As a workaround, I used the following piece of code:

protected override CreateParams CreateParams {
    get {
        CreateParams p = base.CreateParams;
        p.ClassName = "SysTreeView32";
        p.ClassStyle = 8;
        p.ExStyle = 512;
        p.Style = 1442914336;
        return p;

I copied the exact CreateParams of the standard TreeView. This actually creates the window of a TreeView and implements the functionality of ScrollableControl. Now, Windows paints the borders exactly like a TreeView and the contents are custom-drawn.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Vlad Untu
Web Developer
Romania Romania
No Biography provided

You may also be interested in...

Comments and Discussions

Generalplease, for Pin
alhambra-eidos13-Oct-08 3:14
memberalhambra-eidos13-Oct-08 3:14 
Generalnot good Pin
MP3Observer5-Dec-06 6:10
memberMP3Observer5-Dec-06 6:10 
AnswerRe: not good Pin
airfire13-Dec-06 4:57
memberairfire13-Dec-06 4:57 
QuestionCode in VSC# 2003 Pin
Eka Permana11-Jun-06 10:04
memberEka Permana11-Jun-06 10:04 
AnswerRe: Code in VSC# 2003 Pin
airfire13-Dec-06 4:53
memberairfire13-Dec-06 4:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171207.1 | Last Updated 10 Apr 2006
Article Copyright 2006 by Vlad Untu
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid