Add a treeView1 control to WinForm of C #.

Source: Internet
Author: User

Use the tag attribute placement encoding of TreeNode and the Text attribute placement name of TreeNode. The simplest example is as follows: add two nodes first and then process the AfterSelect event:
Using System;
Using System. Drawing;
Using System. Collections;
Using System. ComponentModel;
Using System. Windows. Forms;
Using System. Data;

Namespace testtree
{
/// <Summary>
/// Summary of Form1.
/// </Summary>
Public class Form1: System. Windows. Forms. Form
{
Private System. Windows. Forms. TreeView treeView1;
/// <Summary>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. Container components = null;

Public Form1 ()
{
//
// Required for Windows Form Designer support
//
InitializeComponent ();

//
// TODO: add Any constructor code after InitializeComponent calls
//
TreeNode tn = new TreeNode ();
Tn. Tag = "1001 ";
Tn. Text = "hello ";

TreeNode tn2 = new TreeNode ();
Tn2.Tag = "1002 ";
Tn2.Text = "OK ";

Tn. Nodes. Add (tn2 );

TreeView1.Nodes. Add (tn );
}

/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}

# Region code generated by Windows Form Designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. treeView1 = new System. Windows. Forms. TreeView ();
This. SuspendLayout ();
//
// TreeView1
//
This. treeView1.ImageIndex =-1;
This. treeView1.Location = new System. Drawing. Point (16, 8 );
This. treeView1.Name = "treeView1 ";
This. treeView1.SelectedImageIndex =-1;
This. treeView1.TabIndex = 0;
This. treeView1.AfterSelect + = new System. Windows. Forms. TreeViewEventHandler (this. treeview=afterselect );
//
// Form1
//
This. AutoScaleBaseSize = new System. Drawing. Size (6, 14 );
This. ClientSize = new System. Drawing. Size (292,266 );
This. Controls. Add (this. treeView1 );
This. Name = "Form1 ";
This. Text = "Form1 ";
This. ResumeLayout (false );

}
# Endregion

/// <Summary>
/// Main entry point of the application.
/// </Summary>
[STAThread]
Static void Main ()
{
Application. Run (new Form1 ());
}

Private void treeviewappsafterselect (object sender, System. Windows. Forms. TreeViewEventArgs e)
{
String s = treeView1.SelectedNode. Tag. ToString ();
String s2 = treeView1.SelectedNode. Text. ToString ();
}
}
}
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.