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.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.