Você está na página 1de 4

World's Greatest C# Community

Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Sourc e Code Advertise About

[ Sign In / register ]
P oints N otes M y Forums M y T utorials M y P rofile

Chapter: UnCategorized Current Lesson: IE TreeView Control: Directory Tree [prev. Lesson] Cryptography IE TreeView Control: Directory Tree by: mosessaur IE TreeView Control: Directory Tree by: mosessaur

[Latest Content]
A | B | C| D| E| F | G | H| I | J | K | L | M| N| O | P | Q | R | S| T| U| V | W | X | Y | Z | ALL

Today's Top Movers

[next Lesson] Changing Windows 2000 Password in C#

vulpes 6800 MadHatter 2220 jal 867 Jeff1203 857 muster 791
Yesterday Top Movers

Learn Articles QuickStarts C# Spe c W hitepap ers Tools Class Browser C# Co de Generato r Link s Misc R ss Fee ds Code Highlig ht 411 Dire cto ry FREE m agazines freevb .ne t

Introduction In this Tutorial we will talk about one of the Controls made by Internet Explorer Team, it is TreeView Control. This Control Support booth uplevel& down-level Browsers, that means when a page is requested by IE 5.5 or higher, the control uses DHTML behaviors, but when it is requested by any other browser, the control renders standard HTML 3.2 content. So it is recommended to use IE 5.5 or higher to work with this tutorial. Requirement: First you will need to download the control from here. you can download three controls: 1)TreeView Control, our tutorial

shakti sin.. 9 MadHatter 3 Al_Pennywo.. 2 C#fanatic 2 garyweik1 1


Monthly Leaders

Reviews ASP .NET Hosting Source Code


Ge t Ve rsion 1.0

vulpes 6800 MadHatter 2260 jal 867 Jeff1203 857 muster 791
Top Members

2)TapStrip Control 3)Toolbar Control after you download it, install them, after that you can use them from your Visual Studio by doing the following: 1)From the tools menu Select Customize Toolbox.

mosessaur 18457 Rincewind 7074 stanleytan 6995 vulpes 6800 Gsuttie 6046

C# Consul ti ng AspDotNetStoreFront

2)From the dialog Select .Net Framework Components. 3)find your TreeView Control (The WebControl one) and click on the check box. you can do the same for the TapStrip & Toolbar controls to add Great Offers .net hosti ng then to your Toolbox. Go T o My Pc Notepad and other Editor users You can use these controls by adding the following Directives: <%Import Namespace="Microsoft.Web.UI.WebControls"%> <%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %> you can change the Tag Prefix to anything you want, but be sure to use it when you creat your control. Building Simple Tree View <%Import Namespace="Microsoft.Web.UI.WebControls"%> <%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %>
Remote Pc Control zoneal arm spam bl ocker web hosti ng di rectory ad server C# snadtech GoT oMyPc

<form id="frmIETreeView" runat="server"> <iewc:TreeView runat="server"> <TreeNode Text="Root"> <TreeNode Text="Child 1 of Root" /> <TreeNode Text="Child 2 of Root" /> </TreeNode> </iewc:TreeView> </form>

This was a very simpe Tree, We used the Prefix(iewc) to Create the TreeView cause we registered it at the top. TreeNode Element Present a tree node in the TreeView. TreeNode has a lot of properties like CheckBox Property, Text, Type etc... What you will learn in this Tutorial (1)Using TreeView with some onf its elements and properties, for more information about all elements and properties review the documentation attached with controls or visit: click here (2)Also you will work programaticly with the TreeView Classes to create TreeNodes (3)Using DirectoryInof Class in System.IO (4)Using FileInfo Class in System.IO Tutorial Simply our tutorial will create a tree view for a given Directory. The Hierarchy of the Directory with all sub-directories and Files, included files & directoreis in the sub-directories. We will see how to add icons to the folders and Files, in both Expanded & collapse states. Screen Shot:

Create The Base for our Tree View What I mean by the base is creating the Root element & specify node types: documents, images, aspx files etc.... here is the code: <form id="frmIETreeView" method="post" runat="server"> <iewc:TreeView id="treeDirectory"runat="server" ImageUrl="images/dir.gif" ExpandedImageUrl="images/dir_open.gif"> <iewc:TreeNode Text="Root" ID="RootNode"></iewc:TreeNode> <iewc:TreeNodeType ImageUrl="images/text.gif" ID="Document" Type="Document"> </iewc:TreeNodeType> <iewc:TreeNodeType ImageUrl="images/xmldoc.gif" ID="xmlDoc" Type="XmlDoc"> </iewc:TreeNodeType> <iewc:TreeNodeType ImageUrl="images/aspxdoc.gif" ID="aspxDoc" Type="AspxDoc"> </iewc:TreeNodeType> <iewc:TreeNodeType ImageUrl="images/vbdoc.gif" ID="VBDoc"Type="vbDoc"> </iewc:TreeNodeType> <iewc:TreeNodeType ImageUrl="images/csdoc.gif" ID="CSDoc"Type="csDoc"> </iewc:TreeNodeType> <iewc:TreeNodeType ImageUrl="images/resx.gif" ID="ResxDoc" Type="resxDoc"> </iewc:TreeNodeType> <iewc:TreeNodeType ImageUrl="images/img.gif" ID="Image" Type="img"> </iewc:TreeNodeType>

</iewc:TreeView> </form>

As you can see our Tree has properties imageUrl for the collapes state & ExpandedImageUrl for Expande state. Also one Root element built with TreeNode element, & 5 types of nodes defined with TreeNodeType element. TreeNodeType Element is used to define node types, which mean that you may have about 50 TreeNode that represent Directories, instead of setting thier imageUrl Property in every node alone, you can just define a TreeNodeType Element with imageUrl Property & Type Property string, then set the TreeNode element Type Property to the Specified type you defiend, Next you will see how we do that programaticly. CodeBehind We have only one Method that Populate our TreeView called PopulateTreeView This Method is Recursive Method. let's see we do that: private void PopulateTreeView(string strDirectory, TreeNode nodeParent) { try { //Create A new Instance from DirectoryInfo, to Get information //About The current directory strDirectory DirectoryInfo CurDir = new DirectoryInfo(strDirectory); //Array Holds Sub-Directoies and thier Information DirectoryInfo[] DirArray; //Array Holds Files and thier information FileInfo[] FilesArray; /* * Call GetDirectories() returns and array of Sub-Directories inside * the Current Directory strDirectory */ DirArray = CurDir.GetDirectories(); /* * Call GetFiles() returns and array of Files inside * the Current Directory strDirectory */ FilesArray = CurDir.GetFiles(); //Get every FileInfo inside the current Directory foreach (FileInfo curfileInfo in FilesArray) { //Create New TreeNode like <TreeNode></TreeNode> TreeNode newFileNode = new TreeNode(); //Set the Text Property to The name of the file

newFileNode.Text = curfileInfo.Name; /* *Check for File Extension to set Type Property of the TreeNode. *Check the .aspx File to Check Type Property of each TreeNodeType */ if(curfileInfo.Extension == ".xml") { newFileNode.Type = "XmlDoc"; } else if(curfileInfo.Extension == ".aspx") { newFileNode.Type = "AspxDoc"; } else if(curfileInfo.Extension == ".vb") { newFileNode.Type = "vbDoc"; } else if(curfileInfo.Extension == ".cs") { newFileNode.Type = "csDoc"; } else if(curfileInfo.Extension == ".gif") { newFileNode.Type = "img"; } else if(curfileInfo.Extension == ".resx") { newFileNode.Type = "resxDoc"; } else { newFileNode.Type = "Document"; } //Add the node to its Parent nodeParent.Nodes.Add(newFileNode); } //Get every DirectoryInfo inside the current Directory foreach (DirectoryInfo curdirInfo in DirArray) { TreeNode newDirNode = new TreeNode(); newDirNode.Text = curdirInfo.Name; string strDir; //Full Path strDir = curdirInfo.FullName; //Add The Directory as a child to its parent nodeParent.Nodes.Add(newDirNode); /* *Perform the Operation Recusively to get all sub-directoies and *files, Send every directory (strDir) to get its info *Send newDirNode to be parent to all sub-directories & files *in strDir */ PopulateTreeView(strDir, newDirNode); } } catch(UnauthorizedAccessException) { TreeNode node = new TreeNode(); node.Text = "Access Denied!"; nodeParent.Nodes.Add(node); } } This was all about PopulateTreeView Method, easy, isn't it??!!. This method is called in the Page_Load method: private void Page_Load(object sender, System.EventArgs e) { //1st Param:Send the Current Directory of the project as Current Direcotry //2nd Param:Put all new nodes as children to the Root element PopulateTreeView(MapPath(""), treeDirectory.GetNodeFromIndex("0")); } GetNodeFromIndex Method of the TreeView Control recieves a string as an argument; and Exmaple: "0" : root "0.0" : 1st child of the root "0.1" : 2nd Child

"0.0.0" : 1st child of the 1st child of the root and so on... Visual Basic.Net code The Files is attached to the Tutorials, and also I included the VB.Net version of this Tutorial to the attached file. Download the code MosessaurTreeView.zip

1
Chapter: UnCategorized Current Lesson: IE TreeView Control: Directory Tree [prev. Lesson] Cryptography
Top of Pag e
A dverti s e | A bout | L ink T o U s | P rivac y N otic e C opyright 2 0 0 3 - 2 0 0 5 C S harpFriends .c om A ll Rights Res erved V is ual C # D eveloper C enter

[Latest Content]
A | B | C| D| E| F | G | H| I | J | K | L | M| N| O | P | Q | R | S| T| U| V | W | X | Y | Z | ALL

[next Lesson] Changing Windows 2000 Password in C#

Você também pode gostar