5,693,062 members and growing! (20,360 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Custom Controls     Intermediate License: The Code Project Open License (CPOL)

Button TreeView Control

By milton cse00

Button Tree View is a Tree View Control where nodes are not label control or text box control nodes but button Control
C# 1.0, C#, Windows, .NET, .NET 1.1VS.NET2003, Visual Studio, Dev

Posted: 3 Jan 2007
Updated: 27 Sep 2008
Views: 12,701
Bookmarked: 7 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 2.34 Rating: 2.76 out of 5
2 votes, 28.6%
1
0 votes, 0.0%
2
2 votes, 28.6%
3
1 vote, 14.3%
4
2 votes, 28.6%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Download sourcecode

Demo Application

Sample screenshot

Button Tree View User Control

Tree View control provides a tree-like view of hierarchical data as its user interface. Underneath, its programming model is based on the familiar tree structure consisting of parent nodes and child nodes. Each node has its own Nodes Collection. Where Node has Text, Tag and Image attributes. Tree Structure is shown in figure 1.

image002.gif

Figure : Tree Structure

Tree View control is mostly used for folder tree where need hierarchy. But TreeView doesn’t support controls like (Button, Combobox, Data Grid ) as Nodes.

As each node can link to other nodes as its children and its children know its parent so we can implement Button TreeView by using two way link list as Node. But if we like the same facility and also that of array then Array List is more preferable.

As Array List is a Collection Object so one Array list Object can contain Array List Collection same as Tree Node. Here Array List is used as Node.

Another important property of Tree is level how it interfere in user interface is discussed later.

In user interface each Node looks like controls (like Button, Textbox or other controls) rather than just image and label. So a class is created that inherits Array list and contain button as control.

class ButtonNode :Arraylist

{

Button NodeButton;

}

Most of the functionality of Tree Node of .NET is provided in Button Node . MiButton is used here as Node Button which is inherited from Button Class

How Button Tree View looks like? That’s mainly depending on level. Root node at 0 level, its child at second level and so on. In user interface how tree will looks that is shown when root and child2 is expanded. We can see here that X location of a node is determined by its level and Y location is determined by its Upper node expanded hierarchy. After the completion of upper node’s expanded hierarchy next node is started and all nodes are arranged in such ways that it looks like Figure 2 as shown bellow.

image004.gif

Figure : User Interface of Tree View

the following shows how child nodes level is set

public void AddChild(ButtonNode btnNode)
{
btnNode.Parent=this;
btnNode.NodePosition=this.Count;
btnNode.Level=this.Level+1;
SetLevel(btnNode);

this.Add(btnNode);
}

public void SetLevel(ButtonNode btnNode)
{

foreach(ButtonNode btn in btnNode)
{
btn.Level=btnNode.Level+1;
SetLevel(btn);
}
}

When a child is added to a node then its level is set according to its parent level.

Class Diagram:

image006.gif

Figure : High Level Class Diagram represent tree control structure.

High level class diagram show the structure of the button tree control and how it is implemented here.

Two Main Function ( Expand And Collapse):

Every node of Control Tree is a object and contains its own state, position, level, contents etc.

Now see what happen when a node is expanded:

Let that the target node has child nodes. So,

  1. When it is expanded the child nodes and its successors whose previous status was expanded those nodes and its successors become expanded
  2. The vertical location of those nodes whose location is after the target node but not target nodes children, increase as the number of nodes and level expanded.
  3. But location of those nodes whose location is before the target node and its predecessors, remain same as before expanded.

The figures show the condition before and after the nodes become expanded.

image008.gif

yellow -->targeted node; orange-->expanded node

Figure: The state of Tree before and after a node becomes expanded.

following Code shows how a target node is expanded

public void ExpandNode(ButtonNode btnNode)
{
for(int i=0;i<btnNode.Count;i++)
{
ButtonNode btn=(ButtonNode)btnNode[i];
btn.NodeButton.Visible=true;
if(btn.Count>0)
btn.IndicatorButton.Visible=true;
XLocation=btnNode.RootPanel.XRootLocation+btn.Level*(IndicatorButton.Width+this.RootPanel.NodeHorizontalSeparator);
YLocation=YLocation+NodeButton.Height+RootPanel.NodeVerticalSeparator;
btn.SetLocation(new Point(XLocation,YLocation));
if(btn.Expanded==true)
ExpandNode(btn);
ExpandCount+=1;
}

}

its expanding each nodes under target node. and making its visibility true.

What happens when a node is collapsed?

This is as opposite as the operation of when a node is expanded.

Let that the target node has child nodes whose state is expanded:

  1. When the target node is collapse then all its child nodes become disappear but their status remains same.
  2. The vertical location of those nodes whose location is after the target node but not target nodes children, decrease as the number of nodes and level collapsed.
  3. But location of those nodes whose location is before the target node and its predecessors, remain same as before collapse.

The figures show the condition before and after the nodes become collapsed.

image010.gif

yellow -->targeted node; orange-->collapsed node

Figure : The state of Tree before and after a node becomes collapsed.

following code shows how a node is colloapsed

public void CollapseNode(ButtonNode btnNode)
{
foreach(ButtonNode btn in btnNode)
{

btn.NodeButton.Visible=false;
btn.IndicatorButton.Visible=false;
ExpandCount+=1;
CollapseNode(btn);

}
}

its making nodes visibility false.

Now see what visual functionality is provided here that make easy for rendering.

image011.png

Figure : Tree nodes Basic Property

Here two categories is used one for button tree and another for tree nodes. Tree node property contains button size, node and indicator separations, and indicator images, next node horizontal and vertical separator. Actually these are the basic property for rendering.

So that any control can be provided as tree nodes control and we can control rendering aspects as whole.

image013.png

Figure : An Example of Button Tree Control

the following code shows nodes can be added to Button Tree Control

1. first create nodes

ButtonNode btnNode=new ButtonNode();
ButtonNode btnNode2=new ButtonNode();

2. Set Heirarchy

btnNode.AddChild(btnNode2);

btnNode.NodeButton.Text="Milton";
btnNode2.NodeButton.Text="Syed Md. Abul Bashar";

3. Add Node to the Tree

panel1.Add(btnNode);

So if you think that you need combobox not button as node or you think I need both combobox and button as node that can be done very easily. All the functionality is same and only difference is there is node control which can be independently implemented here. Many of other functionality can be define a but I provide a simple architecture that make it easy to make out.

License

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

About the Author

milton cse00


Name SYED MD. ABUL BASHAR
Email ID: miltoncse00@yahoo.com

I am now working as software engineer in Bangladesh, my mother land; I have completed my B.Sc (Engg.) in CSE from Rajshahi University of Engineering and Technology (RUET).I spend much time in learning latest technology.


B.Sc(Engr) in Computer Science and Engineering CGPA-3.64 (max 4.0)
First Class and Secured 7th place Rajshahi University of Engineering and Technology 2004

Occupation: Software Developer
Location: Bangladesh Bangladesh

Other popular Miscellaneous articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 10 of 10 (Total in Forum: 10) (Refresh)FirstPrevNext
GeneralHow to Add UserControl as TreeNodememberelahe babaee23:24 1 Apr '08  
GeneralRe: How to Add UserControl as TreeNodemembermilton cse0021:49 27 Jun '08  
GeneralDownloading problemmembervinayskvs4:35 31 Oct '07  
GeneralRe: Downloading problemmembermilton cse000:55 9 Nov '07  
GeneralRe: Downloading problemmemberQurex6:58 29 Dec '07  
GeneralHow can I add lines between the nodes as we see in a treeviewmemberpriti arora19:46 5 Mar '07  
GeneralRe: How can I add lines between the nodes as we see in a treeviewmembermilton cse000:32 14 Jun '07  
GeneralNice controlmemberpravat_sagun23:40 13 Jan '07  
GeneralInteresting, but too specificmemberallex4project2:45 4 Jan '07  
GeneralRe: Interesting, but too specificmemberpita20000:14 5 Jan '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 27 Sep 2008
Editor:
Copyright 2007 by milton cse00
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project