C# .NET - tree view - Asked By pravin kumar S on 29-Jul-08 12:47 AM

hi i want to create tree view for window application so any one send  me  details of code to generate tree view

look at here - Web Star replied to pravin kumar S on 29-Jul-08 12:59 AM

public void PopulateTree()
{
   TreeNode nodeProdDetail, nodeProdMaster;
   nodeProdMaster      = new TreeNode();
   nodeProdMaster.Text = "Master1";
   nodeProdMaster.ID   = "1";

   nodeProdDetail             = new TreeNode();
   nodeProdDetail.Text        = "Detail1";
   nodeProdDetail.ID          = "1.1";
   nodeProdDetail.NavigateUrl = "SomePage.aspx";
   nodeProdMaster.Nodes.Add(nodeProdDetail);

   Treeview1.Nodes.Add(nodeProdMaster);

   nodeProdMaster      = new TreeNode();
   nodeProdMaster.Text = "Master2";
   nodeProdMaster.ID   = "2";

   nodeProdDetail             = new TreeNode();
   nodeProdDetail.Text        = "Detail2";
   nodeProdDetail.ID          = "2.1";
   nodeProdDetail.NavigateUrl = "SomeOtherPage.aspx";
   nodeProdMaster.Nodes.Add(nodeProdDetail);

   Treeview1.Nodes.Add(nodeProdMaster);
}

http://www.codeproject.com/KB/cs/TreeViewFileExplorer.aspx

Treeview - Kalit Sikka replied to pravin kumar S on 29-Jul-08 01:04 AM

TreeView Control Overview (Windows Forms)

With the Windows Forms http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.aspx control, you can display a hierarchy of nodes to users, like the way files and folders are displayed in the left pane of the Windows Explorer feature of the Windows operating system. Each node in the tree view might contain other nodes, called child nodes. You can display parent nodes, or nodes that contain child nodes, as expanded or collapsed. You can also display a tree view with check boxes next to the nodes by setting the tree view's http://msdn.microsoft.com/en-us/library/system.windows.forms.treeview.checkboxes.aspx property to true. You can then programmatically select or clear nodes by setting the node's http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.checked.aspx property to true or false.

namespace TreeViewExample
{
    /// <summary>
    /// Summary description for TreeViewExample.
    /// </summary>
    public class TreeViewExample : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.TreeView treeFood;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public TreeViewExample()
        {
            //
            // 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.treeFood = new System.Windows.Forms.TreeView();
            this.SuspendLayout();
            // 
            // treeFood
            // 
            this.treeFood.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom
                | System.Windows.Forms.AnchorStyles.Left
                | System.Windows.Forms.AnchorStyles.Right);
            this.treeFood.ImageIndex = -1;
            this.treeFood.Location = new System.Drawing.Point(45);
            this.treeFood.Name = "treeFood";
            this.treeFood.SelectedImageIndex = -1;
            this.treeFood.Size = new System.Drawing.Size(284256);
            this.treeFood.TabIndex = 1;
            this.treeFood.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeFood_AfterSelect);
            // 
            // TreeViewExample
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(513);
            this.ClientSize = new System.Drawing.Size(292266);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.treeFood});
            this.Name = "TreeViewExample";
            this.Text = "TreeView Example";
            this.Load += new System.EventHandler(this.TreeViewExample_Load);
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new TreeViewExample());
        }

        private void TreeViewExample_Load(object sender, System.EventArgs e)
        {
            TreeNode node;
            
            node = treeFood.Nodes.Add("Fruits");
            node.Nodes.Add("Apple");
            node.Nodes.Add("Peach");
            
            node = treeFood.Nodes.Add("Vegetables");
            node.Nodes.Add("Tomato");
            node.Nodes.Add("Eggplant");
        }

        private void treeFood_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)
        {
            if (e.Action == TreeViewAction.ByMouse)
            {
                MessageBox.Show(e.Node.FullPath);
            }
        }
    }
}


Tree View - Sagar P replied to pravin kumar S on 29-Jul-08 01:09 AM

This tree view control gives you the ability to control which drive types are displayed to let the user choose directories. A possible scenario is to show only local drives, because your Application scan the selected directories and fill a database with the generated meta data, and it makes no sense to allow directories from removable drives.

 
http://www.codeproject.com/KB/tree/treeviewexplorer.aspx
 
http://www.daniweb.com/forums/thread135076.html
 
http://aspalliance.com/articleViewer.aspx?aId=125&pId=
 
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_23396731.html
 

TreeNode treeNode = new TreeNode();
        treeNode = treeView.Nodes.Add("Parent");

        //Child       
        for (int i = 0 ; i<10; i++)
        {
            TreeNode childNode = New TreeNode();
            childNode.Text = "Chile" + i.ToString;

            //picture
            //childNode.ImageKey = i;

            //Picture when selected
            //childNode.SelectedImageKey = "C" & i;
       
            treeNode.Nodes.Add(childNode);

            //Child's Child Node      
            for (int j = 1; j <5; j++)
            {
                TreeNode childChildNode =  New TreeNode();
                childChildNode.Text = "childChildNode" + j.ToSrting();
                childNode.Nodes.Add(childChildNode);
            }
        }

Best Luck!!!!!!!!!!!!!!!!!!!!!!!!
Sujit.
Tree View - Sagar P replied to pravin kumar S on 29-Jul-08 01:08 AM

This tree view control gives you the ability to control which drive types are displayed to let the user choose directories. A possible scenario is to show only local drives, because your Application scan the selected directories and fill a database with the generated meta data, and it makes no sense to allow directories from removable drives.

TreeNode treeNode = new TreeNode();
        treeNode = treeView.Nodes.Add("Parent");

        //Child       
        for (int i = 0 ; i<10; i++)
        {
            TreeNode childNode = New TreeNode();
            childNode.Text = "Chile" + i.ToString;

            //picture
            //childNode.ImageKey = i;

            //Picture when selected
            //childNode.SelectedImageKey = "C" & i;
       
            treeNode.Nodes.Add(childNode);

            //Child's Child Node      
            for (int j = 1; j <5; j++)
            {
                TreeNode childChildNode =  New TreeNode();
                childChildNode.Text = "childChildNode" + j.ToSrting();
                childNode.Nodes.Add(childChildNode);
            }
        }

http://www.codeproject.com/KB/tree/treeviewexplorer.aspx
 
http://www.daniweb.com/forums/thread135076.html
 
http://aspalliance.com/articleViewer.aspx?aId=125&pId=
 
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_23396731.html
 
Best Luck!!!!!!!!!!!!!!!!!!!!!!!!
Sujit.
See This - Sagar P replied to pravin kumar S on 29-Jul-08 01:09 AM

This tree view control gives you the ability to control which drive types are displayed to let the user choose directories. A possible scenario is to show only local drives, because your Application scan the selected directories and fill a database with the generated meta data, and it makes no sense to allow directories from removable drives.

 
http://www.codeproject.com/KB/tree/treeviewexplorer.aspx
 
http://www.daniweb.com/forums/thread135076.html
 
http://aspalliance.com/articleViewer.aspx?aId=125&pId=
 
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_23396731.html
 

TreeNode treeNode = new TreeNode();
        treeNode = treeView.Nodes.Add("Parent");

        //Child       
        for (int i = 0 ; i<10; i++)
        {
            TreeNode childNode = New TreeNode();
            childNode.Text = "Chile" + i.ToString;

            //picture
            //childNode.ImageKey = i;

            //Picture when selected
            //childNode.SelectedImageKey = "C" & i;
       
            treeNode.Nodes.Add(childNode);

            //Child's Child Node      
            for (int j = 1; j <5; j++)
            {
                TreeNode childChildNode =  New TreeNode();
                childChildNode.Text = "childChildNode" + j.ToSrting();
                childNode.Nodes.Add(childChildNode);
            }
        }

Best Luck!!!!!!!!!!!!!!!!!!!!!!!!
Sujit.
Tree View - Sagar P replied to pravin kumar S on 29-Jul-08 01:10 AM

This tree view control gives you the ability to control which drive types are displayed to let the user choose directories. A possible scenario is to show only local drives, because your Application scan the selected directories and fill a database with the generated meta data, and it makes no sense to allow directories from removable drives.

 
http://www.codeproject.com/KB/tree/treeviewexplorer.aspx
 
http://www.daniweb.com/forums/thread135076.html
 
http://aspalliance.com/articleViewer.aspx?aId=125&pId=
 
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_23396731.html
 
Best Luck!!!!!!!!!!!!!!!!!!!!!!!!
Sujit.