Click here to Skip to main content
15,896,456 members
Articles / Programming Languages / C#

Populate TreeView from Simple "list"

Rate me:
Please Sign up or sign in to vote.
4.40/5 (4 votes)
27 Aug 2012CPOL1 min read 59.2K   1.5K   8  
How to SIMPLY populate TreeView from some sort of "list" variable / object / structure
namespace TreeViewExample
{
   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()
      {
         this.btnPopulate = new System.Windows.Forms.Button();
         this.lbData = new System.Windows.Forms.ListBox();
         this.tvData = new System.Windows.Forms.TreeView();
         this.label1 = new System.Windows.Forms.Label();
         this.SuspendLayout();
         // 
         // btnPopulate
         // 
         this.btnPopulate.Location = new System.Drawing.Point(188, 90);
         this.btnPopulate.Name = "btnPopulate";
         this.btnPopulate.Size = new System.Drawing.Size(71, 43);
         this.btnPopulate.TabIndex = 2;
         this.btnPopulate.Text = "Populate            ->";
         this.btnPopulate.UseVisualStyleBackColor = true;
         this.btnPopulate.Click += new System.EventHandler(this.btnPopulate_Click);
         // 
         // lbData
         // 
         this.lbData.FormattingEnabled = true;
         this.lbData.Items.AddRange(new object[] {
            "0 Movies",
            "1 Comedy",
            "1 SF",
            "2 5th Element",
            "3 Bruce Willis",
            "3 Mila Jovovic",
            "2 Matrix",
            "0 Music",
            "1 Alternative",
            "1 R&R",
            "2 Coldplay",
            "1 Clasic",
            "0 Pictures",
            "1 Trips",
            "2 Europe",
            "3 Stuttgart",
            "4 Mercedes-Benz museum",
            "2 Africa"});
         this.lbData.Location = new System.Drawing.Point(12, 12);
         this.lbData.Name = "lbData";
         this.lbData.Size = new System.Drawing.Size(159, 251);
         this.lbData.TabIndex = 3;
         // 
         // tvData
         // 
         this.tvData.Location = new System.Drawing.Point(274, 12);
         this.tvData.Name = "tvData";
         this.tvData.Size = new System.Drawing.Size(203, 251);
         this.tvData.TabIndex = 4;
         // 
         // label1
         // 
         this.label1.AutoSize = true;
         this.label1.Location = new System.Drawing.Point(9, 266);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(181, 13);
         this.label1.TabIndex = 5;
         this.label1.Text = "ListBox is used because visualisation";
         // 
         // Form1
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(488, 288);
         this.Controls.Add(this.label1);
         this.Controls.Add(this.tvData);
         this.Controls.Add(this.lbData);
         this.Controls.Add(this.btnPopulate);
         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
         this.Name = "Form1";
         this.Text = "Form1";
         this.ResumeLayout(false);
         this.PerformLayout();

      }

      #endregion

      private System.Windows.Forms.Button btnPopulate;
      private System.Windows.Forms.ListBox lbData;
      private System.Windows.Forms.TreeView tvData;
      private System.Windows.Forms.Label label1;
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Yugoslavia Yugoslavia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions