Subclass TreeView : TreeView « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » TreeViewScreenshots 
Subclass TreeView
 

using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
   
class DirectoryTreeView: TreeView
{
     public DirectoryTreeView()
     {
          ImageList = new ImageList();
          ImageList.Images.Add(new Bitmap(GetType()"FLOPPY.BMP"));
          ImageList.Images.Add(new Bitmap(GetType()"FOLD.BMP"));
          ImageList.Images.Add(new Bitmap(GetType()"OPENFOLD.BMP"));
          RefreshTree();
     }
     public void RefreshTree()
     {
          BeginUpdate();
          Nodes.Clear();
          string[] astrDrives = Directory.GetLogicalDrives();
   
          foreach (string str in astrDrives)
          {
               TreeNode tnDrive = new TreeNode(str, 00);
               Nodes.Add(tnDrive);
               AddDirectories(tnDrive);
   
               if (str == "C:\\")
                    SelectedNode = tnDrive;
          }
          EndUpdate();
     }
     void AddDirectories(TreeNode tn)
     {
          tn.Nodes.Clear();
   
          string          strPath = tn.FullPath;
          DirectoryInfo   dirinfo = new DirectoryInfo(strPath);
          DirectoryInfo[] adirinfo;
   
          adirinfo = dirinfo.GetDirectories();
   
          foreach (DirectoryInfo di in adirinfo)
          {
               TreeNode tnDir = new TreeNode(di.Name, 12);
               tn.Nodes.Add(tnDir);
          }
     }
     protected override void OnBeforeExpand(TreeViewCancelEventArgs tvcea)
     {
          base.OnBeforeExpand(tvcea);
   
          BeginUpdate();
   
          foreach (TreeNode tn in tvcea.Node.Nodes)
               AddDirectories(tn);
   
          EndUpdate();
     }
}

class DirectoriesAndFiles: Form
{
     DirectoryTreeView dirtree;
     Panel             panel;
     TreeNode          tnSelect;
   
     public static void Main()
     {
          Application.Run(new DirectoriesAndFiles());
     }
     public DirectoriesAndFiles()
     {
          Text = "Directories and Files";
          BackColor = SystemColors.Window;
          ForeColor = SystemColors.WindowText;
   
          panel = new Panel();
          panel.Parent = this;
          panel.Dock = DockStyle.Fill;
          panel.Paint += new PaintEventHandler(PanelOnPaint);
   
          Splitter split = new Splitter();
          split.Parent = this;
          split.Dock = DockStyle.Left;
          split.BackColor = SystemColors.Control;
   
          dirtree = new DirectoryTreeView();
          dirtree.Parent = this;
          dirtree.Dock = DockStyle.Left;
          dirtree.AfterSelect += new TreeViewEventHandler(DirectoryTreeViewOnAfterSelect);
   
          Menu = new MainMenu();
          Menu.MenuItems.Add("View");
   
          MenuItem mi = new MenuItem("Refresh"new EventHandler(MenuOnRefresh), Shortcut.F5);
          Menu.MenuItems[0].MenuItems.Add(mi);
     }
     void DirectoryTreeViewOnAfterSelect(object obj, TreeViewEventArgs tvea)
     {
          tnSelect = tvea.Node;
          panel.Invalidate();
     }
     void PanelOnPaint(object obj, PaintEventArgs pea)
     {
          if (tnSelect == null)
               return;
   
          Panel         panel     = (Panelobj;
          Graphics      grfx      = pea.Graphics;
          DirectoryInfo dirinfo   = new DirectoryInfo(tnSelect.FullPath);
          FileInfo[]    afileinfo;
          Brush         brush     = new SolidBrush(panel.ForeColor);
          int           y         = 0;
   
          afileinfo = dirinfo.GetFiles();
          foreach (FileInfo fileinfo in afileinfo)
          {
               grfx.DrawString(fileinfo.Name, Font, brush, 0, y);
               y += Font.Height;
          }
     }
     void MenuOnRefresh(object obj, EventArgs ea)
     {
          dirtree.RefreshTree();
     }
}

 
Related examples in the same category
1.Recursively load Directory info into TreeViewRecursively load Directory info into TreeView
2.Drag and drop Tree NodeDrag and drop Tree Node
3.Get Selected Node Full PathGet Selected Node Full Path
4.Custom TreeView
5.TreeView ExampleTreeView Example
6.TreeView Drag And DropTreeView Drag And Drop
7.TreeView Data BindingTreeView Data Binding
8.Read an XML Document and display the file as a TreeRead an XML Document and display the file as a Tree
9.TreeView DemoTreeView Demo
10.Directory Tree HostDirectory Tree Host
11.Add Nodes to TreeView
12.TreeView ImageIndex
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.