Custom 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 
Custom TreeView

/*
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 Disposebool disposing )
        {
            ifdisposing )
            {
                if (components != null
                {
                    components.Dispose();
                }
            }
            base.Disposedisposing );
        }

        #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(1616);
//            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(84);
            this.tree.Name = "tree";
            this.tree.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
                                                                             new System.Windows.Forms.TreeNode("Unassigned"00),
                                                                             new System.Windows.Forms.TreeNode("In Progress"11),
                                                                             new System.Windows.Forms.TreeNode("Closed"22)});
            this.tree.Scrollable = false;
            this.tree.Size = new System.Drawing.Size(320296);
            this.tree.TabIndex = 0;
            // 
            // CustomTreeView
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(513);
            this.ClientSize = new System.Drawing.Size(336310);
            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"00);
            private TreeNode nodeInProgress = new TreeNode("In Progress"11);
            private TreeNode nodeClosed = new TreeNode("Closed"22);

            // 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, 34);
                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"00);
        private TreeNode nodeUsers = new TreeNode("Users"11);

        // 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()23);
            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()23);
            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);
                 }

             }
    }
}


           
       
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.TreeView ExampleTreeView Example
5.TreeView Drag And DropTreeView Drag And Drop
6.TreeView Data BindingTreeView Data Binding
7.Read an XML Document and display the file as a TreeRead an XML Document and display the file as a Tree
8.TreeView DemoTreeView Demo
9.Directory Tree HostDirectory Tree Host
10.Subclass TreeView
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.