/* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457 */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace CustomTreeView { /// <summary> /// Summary description for Form1. /// </summary> public class CustomTreeView : System.Windows.Forms.Form { internal System.Windows.Forms.ImageList imagesTree; private ProjectTree tree; private System.ComponentModel.IContainer components; public CustomTreeView() { // // 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() { this.components = new System.ComponentModel.Container(); // System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(CustomTreeView)); this.imagesTree = new System.Windows.Forms.ImageList(this.components); this.tree = new ProjectTree(); this.SuspendLayout(); // // imagesTree // this.imagesTree.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit; this.imagesTree.ImageSize = new System.Drawing.Size(16, 16); // this.imagesTree.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imagesTree.ImageStream"))); this.imagesTree.TransparentColor = System.Drawing.Color.Transparent; // // tree // this.tree.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.tree.ImageList = this.imagesTree; this.tree.Location = new System.Drawing.Point(8, 4); this.tree.Name = "tree"; this.tree.Nodes.AddRange(new System.Windows.Forms.TreeNode[] { new System.Windows.Forms.TreeNode("Unassigned", 0, 0), new System.Windows.Forms.TreeNode("In Progress", 1, 1), new System.Windows.Forms.TreeNode("Closed", 2, 2)}); this.tree.Scrollable = false; this.tree.Size = new System.Drawing.Size(320, 296); this.tree.TabIndex = 0; // // CustomTreeView // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(336, 310); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.tree}); this.Name = "CustomTreeView"; this.Text = "ProjectUserTree"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new CustomTreeView()); } private void Form1_Load(object sender, System.EventArgs e) { tree.AddProject("Migration to .NET", ProjectTree.StatusType.InProgress); tree.AddProject("Revamp pricing site", ProjectTree.StatusType.Unassigned); tree.AddProject("Prepare L-DAP feasibility report", ProjectTree.StatusType.Unassigned); tree.AddProject("Update E201-G to Windows XP", ProjectTree.StatusType.Closed); tree.AddProject("Annual meeting", ProjectTree.StatusType.Closed); } } public class ProjectTree : TreeView { // Use an enumeration to represent the three types of nodes. // Specific numbers correspond to the database field code. public enum StatusType { Unassigned = 101, InProgress = 102, Closed = 103 } // Store references to the three main node branches. private TreeNode nodeUnassigned = new TreeNode("Unassigned", 0, 0); private TreeNode nodeInProgress = new TreeNode("In Progress", 1, 1); private TreeNode nodeClosed = new TreeNode("Closed", 2, 2); // Add the main level of nodes when the control is instantiated. public ProjectTree() : base() { base.Nodes.Add(nodeUnassigned); base.Nodes.Add(nodeInProgress); base.Nodes.Add(nodeClosed); } // Provide a specialized method the client can use to add nodes. public void AddProject(string name, StatusType status) { TreeNode nodeNew = new TreeNode(name, 3, 4); nodeNew.Tag = status; switch (status) { case StatusType.Unassigned: nodeUnassigned.Nodes.Add(nodeNew); break; case StatusType.InProgress: nodeInProgress.Nodes.Add(nodeNew); break; case StatusType.Closed: nodeClosed.Nodes.Add(nodeNew); break; } } } public class ProjectUserTree : TreeView { // Use an enumeration to represent the three types of nodes. public enum NodeType { Project, User } // Define a new type of higher-level event for node selection. public delegate void ItemSelectEventHandler(object sender, ItemSelectEventArgs e); public class ItemSelectEventArgs : EventArgs { public NodeType Type; public DataRow ItemData; } // Define the events that use this signature and event arguments. public event ItemSelectEventHandler UserSelect; public event ItemSelectEventHandler ProjectSelect; // Store references to the two main node branches. private TreeNode nodeProjects = new TreeNode("Projects", 0, 0); private TreeNode nodeUsers = new TreeNode("Users", 1, 1); // Add the main level of nodes when the control is instantiated. public ProjectUserTree() : base() { base.Nodes.Add(nodeProjects); base.Nodes.Add(nodeUsers); } // Provide a specialized method the client can use to add projects. // Store the corresponding DataRow. public void AddProject(DataRow project) { TreeNode nodeNew = new TreeNode(project["Name"].ToString(), 2, 3); nodeNew.Tag = project; nodeProjects.Nodes.Add(nodeNew); } // Provide a specialized method the client can use to add users. // Store the correspnding DataRow. public void AddUser(DataRow user) { TreeNode nodeNew = new TreeNode(user["Name"].ToString(), 2, 3); nodeNew.Tag = user; nodeUsers.Nodes.Add(nodeNew); } // When a node is selected, retrieve the DataRow and raise the event. protected override void OnAfterSelect(TreeViewEventArgs e) { base.OnAfterSelect(e); ItemSelectEventArgs arg = new ItemSelectEventArgs(); arg.ItemData = (DataRow)e.Node.Tag; if (e.Node.Parent == nodeProjects) { arg.Type = NodeType.Project; ProjectSelect(this, arg); } else if (e.Node.Parent == nodeUsers) { arg.Type = NodeType.User; UserSelect(this, arg); } } } }