Click here to Skip to main content
Click here to Skip to main content
Go to top

TreeView filter

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

Sample Image

Introduction

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.

License

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

Share

About the Author

Vlad Untu
Web Developer
Romania Romania
No Biography provided

Comments and Discussions

 
Generalplease, for asp.net Pinmemberalhambra-eidos13-Oct-08 2:14 
Generalnot good PinmemberMP3Observer5-Dec-06 5:10 
AnswerRe: not good Pinmemberairfire13-Dec-06 3:57 
QuestionCode in VSC# 2003 PinmemberEka Permana11-Jun-06 9:04 
AnswerRe: Code in VSC# 2003 Pinmemberairfire13-Dec-06 3:53 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 10 Apr 2006
Article Copyright 2006 by Vlad Untu
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid