Winform Control Learning Notes [day 6] -- TreeView

Source: Internet
Author: User

 

 

Edit Selected Node

This method takes effect only when the LabelEdit attribute of TreeView is set to true. If LabelEdit is set to false, an exception is thrown and the tree node is not editable. The corresponding method is EndEdit () to close the editing node.

treeView1.LabelEdit = true;treeView1.SelectedNode.BeginEdit();

 

Delete selected node

treeView1.Nodes.Remove(treeView1.SelectedNode);

 

Expand selected nodes

treeView1.SelectedNode.ExpandAll();

 

Collapse selected nodes

If the Boolean parameter passed by the method is true, the subnode remains in the current State. If the value is false, the subnode is collapsed.

treeView1.SelectedNode.Collapse(false);

 

Switch the folding status of selected nodes

treeView1.SelectedNode.Toggle();

 

Show check box

It is associated with the Checked attribute to obtain or set a value to indicate whether the tree node is selected.

treeView1.Check

 

Select Node path

treeView1.SelectedNode.FullPath

 

Depth of the selected node tree

treeView1.SelectedNode.Level;

Number of Tree nodes

treeView1.GetNodeCount(true);

 

For detailed use, refer to MSDN: http://msdn.microsoft.com/zh-cn/library/system.windows.forms.treenode (v = vs.100). aspx

 

Instance 1.The TreeView control displays local disks and folders.

Disk traversal code:

// Source: http://zxlovenet.cnblogs.com Private void ForeachDisk () {DriveInfo [] dr = DriveInfo. getDrives (); string driveName = ""; foreach (DriveInfo d in dr) {switch (d. driveType) {case DriveType. fixed: driveName = "Local disk (" + d. name. substring (0, 2) + ")"; break; case DriveType. removable: driveName = "Removable disk (" + d. name. substring (0, 2) + ")"; break; case DriveType. CDRom: driveName = "DVD drive (" + d. name. substring (0, 2) + ")"; break; case DriveType. network: driveName = "Network drive (" + d. name. substring (0, 2) + ")"; break; default: driveName = "unknown (" + d. name + ")"; break;} this. treeView1.Nodes. add (d. name, driveName );}}

 

Show folder content:

// Source: http://zxlovenet.cnblogs.com Private void ReadDir (TreeNodeMouseClickEventArgs e) {if (e. node. nodes. count> 0) {if (e. node. isExpanded) {e. node. collapse ();} else {e. node. expand () ;}} else {if (Directory. exists (e. node. name) {try {string [] allDirectory = Directory. getDirectories (e. node. name); foreach (string s in allDirectory) {e. node. nodes. add (s, s. remove (0, s. lastIndexOf ("//") + 1);} listBox1.Items. clear (); string [] allFiles = Directory. getFiles (e. node. name); foreach (string sf in allFiles) {listBox1.Items. add (sf. remove (0, sf. lastIndexOf ("//") + 1);} catch {}} e. node. expand ();}}

 

Instance 2.TreeView for drag and drop operations

Background code:

// Source: http://zxlovenet.cnblogs.com Private Point Position = new Point (0, 0); private void Form1_Load (object sender, EventArgs e) {this. treeView1.AllowDrop = true; this. treeView1.ExpandAll ();} private void treeviewincluitemdrag (object sender, ItemDragEventArgs e) {DoDragDrop (e. item, DragDropEffects. move);} private void treeviewincludragenter (object sender, DragEventArgs e) {if (e. data. getDataPresent (typeof (TreeNode) e. effect = DragD RopEffects. move; else e. effect = DragDropEffects. none;} private void treeviewincludragdrop (object sender, DragEventArgs e) {TreeNode myNode = null; if (e. data. getDataPresent (typeof (TreeNode) {myNode = (TreeNode) (e. data. getData (typeof (TreeNode);} else {MessageBox. show ("error");} Position. X = e. x; Position. Y = e. y; Position = treeView1.PointToClient (Position); TreeNode DropNode = this. treeVie W1.GetNodeAt (Position); // 1. The target node is not empty. 2. The target node is not the byte point of the dragged contact. 3. if (DropNode! = Null & DropNode. Parent! = MyNode & DropNode! = MyNode) {TreeNode DragNode = myNode; // The dragged node is deleted from the original position. MyNode. remove (); // Add the DropNode to the target node. nodes. add (DragNode);} // if the target node does not exist, that is, if the drag position does not exist, place the dragged node under the root node if (DropNode = null) {TreeNode DragNode = myNode; myNode. remove (); treeView1.Nodes. add (DragNode );}}

 

Download: DEMO

 

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.