using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
private System.Windows.Forms.TreeView treeView1;
ImageList il = new ImageList();
public Form1() {
this.treeView1 = new System.Windows.Forms.TreeView();
this.SuspendLayout();
this.treeView1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right);
this.treeView1.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.treeView1.HotTracking = true;
this.treeView1.ImageIndex = -1;
this.treeView1.Indent = 30;
this.treeView1.ItemHeight = 30;
this.treeView1.LabelEdit = true;
this.treeView1.Location = new System.Drawing.Point(8, 16);
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(360, 272);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(376, 309);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.treeView1});
this.Text = "TreeView Control";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e) {
il.Images.Add(new Icon("1.ICO"));
il.Images.Add(new Icon("2.ICO"));
il.Images.Add(new Icon("3.ICO"));
il.Images.Add(new Icon("4.ICO"));
treeView1.ImageList = il;
TreeNode rootNode = treeView1.Nodes.Add("USA");
rootNode.ImageIndex = 0;
TreeNode states1 = rootNode.Nodes.Add("a");
states1.ImageIndex = 1;
TreeNode states2 = rootNode.Nodes.Add("b");
states2.ImageIndex = 1;
TreeNode states3 = rootNode.Nodes.Add("c");
states3.ImageIndex = 1;
TreeNode states4 = rootNode.Nodes.Add("d");
states4.ImageIndex = 1;
TreeNode child = states1.Nodes.Add("A");
child.ImageIndex = 2;
child = states1.Nodes.Add("e");
child.ImageIndex = 2;
child = states1.Nodes.Add("f");
child.ImageIndex = 2;
child = states2.Nodes.Add("g");
child.ImageIndex = 2;
child = states2.Nodes.Add("h");
child.ImageIndex = 2;
child = states2.Nodes.Add("i");
child.ImageIndex = 2;
child = states3.Nodes.Add("j");
child.ImageIndex = 2;
child = states3.Nodes.Add("k");
child.ImageIndex = 2;
child = states3.Nodes.Add("l");
child.ImageIndex = 2;
child = states4.Nodes.Add("m");
child.ImageIndex = 2;
child = states4.Nodes.Add("n");
child.ImageIndex = 2;
child = states4.Nodes.Add("o");
child.ImageIndex = 2;
}
}
Month: February 2011
Unicode encoding: English
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Unicode : System.Windows.Forms.Form {
System.Windows.Forms.Label myLabel = new System.Windows.Forms.Label();
public Unicode()
{
this.SuspendLayout();
this.myLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 50.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.myLabel.Location = new System.Drawing.Point(20, 20);
this.myLabel.Size = new System.Drawing.Size(800, 800);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(800, 200);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.myLabel});
this.Name = "Unicode";
this.Text = "Unicode";
this.Load += new System.EventHandler(this.Unicode_Load);
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new Unicode());
}
private void Unicode_Load(object sender, System.EventArgs e)
{
// English
char[] english = {'u0057', 'u0065', 'u006C',
'u0063', 'u006F', 'u006D', 'u0065', 'u0020',
'u0074', 'u006F', 'u0020' };
myLabel.Text = new string(english) + "Unicode" + 'u0021';
}
}
Unicode encoding: German
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Unicode : System.Windows.Forms.Form {
System.Windows.Forms.Label myLabel = new System.Windows.Forms.Label();
public Unicode()
{
this.SuspendLayout();
this.myLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 50.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.myLabel.Location = new System.Drawing.Point(20, 20);
this.myLabel.Size = new System.Drawing.Size(800, 800);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(800, 200);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.myLabel});
this.Name = "Unicode";
this.Text = "Unicode";
this.Load += new System.EventHandler(this.Unicode_Load);
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new Unicode());
}
private void Unicode_Load(object sender, System.EventArgs e)
{
// German
char[] german = {'u0057', 'u0069', 'u006C',
'u006B', 'u006F', 'u006D', 'u006D', 'u0065',
'u006E', 'u0020', 'u007A', 'u0075', 'u0020'};
myLabel.Text = new string(german) + "Unicode" + 'u0021';
}
}
Unicode encoding: French
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class Unicode : System.Windows.Forms.Form {
System.Windows.Forms.Label myLabel = new System.Windows.Forms.Label();
public Unicode()
{
this.SuspendLayout();
this.myLabel.Font = new System.Drawing.Font("Microsoft Sans Serif", 50.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
this.myLabel.Location = new System.Drawing.Point(20, 20);
this.myLabel.Size = new System.Drawing.Size(800, 800);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(800, 200);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.myLabel});
this.Name = "Unicode";
this.Text = "Unicode";
this.Load += new System.EventHandler(this.Unicode_Load);
this.ResumeLayout(false);
}
[STAThread]
static void Main()
{
Application.Run(new Unicode());
}
private void Unicode_Load(object sender, System.EventArgs e)
{
// French
char[] french = { 'u0042', 'u0069', 'u0065',
'u006E', 'u0076', 'u0065', 'u006E', 'u0075',
'u0065', 'u0020', 'u0061', 'u0075', 'u0020' };
myLabel.Text = new string(french) + "Unicode" + 'u0021';
}
}
Add Nodes to TreeView
using System;
using System.Drawing;
using System.Windows.Forms;
class SimpleTreeView: Form
{
public static void Main()
{
Application.Run(new SimpleTreeView());
}
public SimpleTreeView()
{
Text = "Simple Tree View";
TreeView tree = new TreeView();
tree.Parent = this;
tree.Dock = DockStyle.Fill;
tree.Nodes.Add("Animal");
tree.Nodes[0].Nodes.Add("A");
tree.Nodes[0].Nodes[0].Nodes.Add("A1");
tree.Nodes[0].Nodes[0].Nodes.Add("A2");
tree.Nodes[0].Nodes[0].Nodes.Add("A3");
tree.Nodes[0].Nodes.Add("B");
tree.Nodes[0].Nodes[1].Nodes.Add("B1");
tree.Nodes[0].Nodes[1].Nodes.Add("B2");
tree.Nodes[0].Nodes.Add("C");
tree.Nodes[0].Nodes[2].Nodes.Add("C1");
tree.Nodes[0].Nodes[2].Nodes.Add("C2");
tree.Nodes[0].Nodes[2].Nodes.Add("C3");
tree.Nodes.Add("D");
tree.Nodes[1].Nodes.Add("D1");
tree.Nodes[1].Nodes.Add("D2");
tree.Nodes[1].Nodes.Add("D3");
tree.Nodes.Add("E");
tree.Nodes[2].Nodes.Add("E1");
tree.Nodes[2].Nodes.Add("E2");
tree.Nodes[2].Nodes.Add("E3");
}
}
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, 0, 0);
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, 1, 2);
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 = (Panel) obj;
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();
}
}
Directory Tree Host
/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald
Publisher: Apress
ISBN: 1590590457
*/
using System.IO;
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace DirectoryTreeHost
{
/// <summary>
/// Summary description for DirectoryTreeHost.
/// </summary>
public class DirectoryTreeHost : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public DirectoryTreeHost()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// DirectoryTreeHost
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "DirectoryTreeHost";
this.Text = "DirectoryTreeHost";
this.Load += new System.EventHandler(this.DirectoryTreeHost_Load);
}
#endregion
private void DirectoryTreeHost_Load(object sender, System.EventArgs e)
{
DirectoryTree dirTree = new
DirectoryTree();
dirTree.Size = new Size(this.Width - 30, this.Height - 60);
dirTree.Location = new Point(5, 5);
dirTree.Drive = Char.Parse("C");
this.Controls.Add(dirTree);
}
public static void Main()
{
Application.Run(new DirectoryTreeHost());
}
}
public class DirectoryTree : TreeView
{
public delegate void DirectorySelectedDelegate(object sender,
DirectorySelectedEventArgs e);
public event DirectorySelectedDelegate DirectorySelected;
private Char drive;
public Char Drive
{
get
{
return drive;
}
set
{
drive = value;
RefreshDisplay();
}
}
// This is public so a Refresh can be triggered manually.
public void RefreshDisplay()
{
// Erase the existing tree.
this.Nodes.Clear();
// Set the first node.
TreeNode rootNode = new TreeNode(drive + ":");
this.Nodes.Add(rootNode);
// Fill the first level and expand it.
Fill(rootNode);
this.Nodes[0].Expand();
}
private void Fill(TreeNode dirNode)
{
DirectoryInfo dir = new DirectoryInfo(dirNode.FullPath);
// An exception could be thrown in this code if you don't
// have sufficient security permissions for a file or directory.
// You can catch and then ignore this exception.
foreach (DirectoryInfo dirItem in dir.GetDirectories())
{
// Add node for the directory.
TreeNode newNode = new TreeNode(dirItem.Name);
dirNode.Nodes.Add(newNode);
newNode.Nodes.Add("*");
}
}
protected override void OnBeforeExpand(TreeViewCancelEventArgs e)
{
base.OnBeforeExpand(e);
// If a dummy node is found, remove it and read the real directory list.
if (e.Node.Nodes[0].Text == "*")
{
e.Node.Nodes.Clear();
Fill(e.Node);
}
}
protected override void OnAfterSelect(TreeViewEventArgs e)
{
base.OnAfterSelect(e);
// Raise the DirectorySelected event.
if (DirectorySelected != null)
{
DirectorySelected(this,
new DirectorySelectedEventArgs(e.Node.FullPath));
}
}
}
public class DirectorySelectedEventArgs : EventArgs
{
public string DirectoryName;
public DirectorySelectedEventArgs(string directoryName)
{
this.DirectoryName = directoryName;
}
}
}

