Get indexes of full path selected of a Treeview

Hi,

I would like to get the indexes of each node level when a path is selected from a treeview.

For example, if Level4b from the below tree is selected, it will return 1-0-0-1

This should work similar to e.Node.FullPath except return the indexes rather than the node text.


Level 1a
          Level2a
          Level2b
                    Level3a
Level 1b
          Level2c
                    Level3b
                              Level4a
                              Level4b
Level 1c
          Level2d
                    Level3c
thinklingsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
It's a simple while loop...like this:
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            List<int> indices = new List<int>();

            TreeNode node = e.Node;
            while (node != null)
            {
                indices.Insert(0, node.Index);
                node = node.Parent;
            }

            // do something with "indices":
            string path = String.Join("-", indices);
            Console.WriteLine(path);
        }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
it_saigeDeveloperCommented:
Or you could use recursion:

Form1.cs -
using System.Windows.Forms;

namespace EE_Q28709852
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		private void OnAfterCheck(object sender, TreeViewEventArgs e)
		{
			textBox1.Text = GetIndex(e.Node);
		}

		private string GetIndex(TreeNode node)
		{
			string result = string.Empty;
			if (node.Parent != null)
				result += string.Format("{0}-", GetIndex(node.Parent));
			result += string.Format("{0}", node.Index);
			return result;
		}
	}
}

Open in new window

Form1.Designer.cs -
namespace EE_Q28709852
{
	partial class Form1
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.IContainer components = null;

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing && (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()
		{
			System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("Level2a");
			System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Level3a");
			System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("Level2b", new System.Windows.Forms.TreeNode[] {
            treeNode2});
			System.Windows.Forms.TreeNode treeNode4 = new System.Windows.Forms.TreeNode("Level1a", new System.Windows.Forms.TreeNode[] {
            treeNode1,
            treeNode3});
			System.Windows.Forms.TreeNode treeNode5 = new System.Windows.Forms.TreeNode("Level4a");
			System.Windows.Forms.TreeNode treeNode6 = new System.Windows.Forms.TreeNode("Level4b");
			System.Windows.Forms.TreeNode treeNode7 = new System.Windows.Forms.TreeNode("Level3b", new System.Windows.Forms.TreeNode[] {
            treeNode5,
            treeNode6});
			System.Windows.Forms.TreeNode treeNode8 = new System.Windows.Forms.TreeNode("Level2c", new System.Windows.Forms.TreeNode[] {
            treeNode7});
			System.Windows.Forms.TreeNode treeNode9 = new System.Windows.Forms.TreeNode("Level1b", new System.Windows.Forms.TreeNode[] {
            treeNode8});
			System.Windows.Forms.TreeNode treeNode10 = new System.Windows.Forms.TreeNode("Level3c");
			System.Windows.Forms.TreeNode treeNode11 = new System.Windows.Forms.TreeNode("Level2d", new System.Windows.Forms.TreeNode[] {
            treeNode10});
			System.Windows.Forms.TreeNode treeNode12 = new System.Windows.Forms.TreeNode("Level1c", new System.Windows.Forms.TreeNode[] {
            treeNode11});
			this.treeView1 = new System.Windows.Forms.TreeView();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// treeView1
			// 
			this.treeView1.CheckBoxes = true;
			this.treeView1.Location = new System.Drawing.Point(13, 13);
			this.treeView1.Name = "treeView1";
			treeNode1.Name = "Level2a";
			treeNode1.Text = "Level2a";
			treeNode2.Name = "Level3a";
			treeNode2.Text = "Level3a";
			treeNode3.Name = "Level2b";
			treeNode3.Text = "Level2b";
			treeNode4.Name = "Level1a";
			treeNode4.Text = "Level1a";
			treeNode5.Name = "Level4a";
			treeNode5.Text = "Level4a";
			treeNode6.Name = "Level4b";
			treeNode6.Text = "Level4b";
			treeNode7.Name = "Level3b";
			treeNode7.Text = "Level3b";
			treeNode8.Name = "Level2c";
			treeNode8.Text = "Level2c";
			treeNode9.Name = "Level1b";
			treeNode9.Text = "Level1b";
			treeNode10.Name = "Level3c";
			treeNode10.Text = "Level3c";
			treeNode11.Name = "Level2d";
			treeNode11.Text = "Level2d";
			treeNode12.Name = "Level1c";
			treeNode12.Text = "Level1c";
			this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
            treeNode4,
            treeNode9,
            treeNode12});
			this.treeView1.Size = new System.Drawing.Size(259, 205);
			this.treeView1.TabIndex = 0;
			this.treeView1.AfterCheck += new System.Windows.Forms.TreeViewEventHandler(this.OnAfterCheck);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(13, 224);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(259, 20);
			this.textBox1.TabIndex = 1;
			// 
			// Form1
			// 
			this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
			this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
			this.ClientSize = new System.Drawing.Size(284, 251);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.treeView1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.ResumeLayout(false);
			this.PerformLayout();

		}

		#endregion

		private System.Windows.Forms.TreeView treeView1;
		private System.Windows.Forms.TextBox textBox1;
	}
}

Open in new window

Produces the following output -Capture.JPG-saige-
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.