C # TreeView disk file, resource manager, AfterSelect, display plus sign

Source: Internet
Author: User


As you can see, the plus sign (+) is displayed in front of the item and the files on the disk are read. The Code is as follows:
Main program code:
[Csharp]
<Span style = "font-size: 14px;"> using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using System. IO;
Namespace FileManager
{
Public partial class Main: Form
{
Public Main ()
{
InitializeComponent ();
}
/// <Summary>
/// Load the Logical Disk File
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void Main_Load (object sender, EventArgs e)
{
DriveInfo [] myDrivers = DriveInfo. GetDrives ();
Foreach (DriveInfo di in myDrivers)
{
If (di. IsReady)
{
TreeNode tNode = new TreeNode (di. Name. Split (':') [0]);
TNode. Name = di. Name;
TNode. Tag = tNode. Name;
<Span style = "color: # ff0000;"> tNode </span>. <span style = "color: # ff0000;"> Nodes </span>. <span style = "color: # ff0000;"> Add </span> <span style = "color: # ff0000; "> (" </span> <span style = "color: # ff0000;"> DUMMY </span> <span style = "color: # ff0000;"> "); </span>
TvRoot. Nodes. Add (tNode );
}
}
// Add the right-click event
This. tvRoot. ContextMenuStrip = new TreeViewContextMenu (). Load ();
}
 
/// <Summary>
/// One of the two events that the TreeView must process
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void tvRoot_AfterSelect (object sender, TreeViewEventArgs e)
{
E. Node. Expand ();
}
 
/// <Summary>
/// One of the two events that the TreeView must process
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Private void tvRoot_BeforeExpand (object sender, TreeViewCancelEventArgs e)
{
TreeViewItems. Add (sender, e );
}
}
} </Span>

Code for adding a TreeNode to a TreeView:
[Csharp]
<Span style = "font-size: 14px;"> public static class TreeViewItems
{
Public static void Add (object sender, TreeViewCancelEventArgs e)
{
E. Node. Nodes. Clear ();
TreeNode tNode = e. Node;
// MessageBox. Show (tNode. Name );
String path = tNode. Name;
String [] dics = Directory. GetDirectories (path );
Foreach (string dic in dics)
{
TreeNode subNode = new TreeNode (new DirectoryInfo (dic). Name );
SubNode. Name = new DirectoryInfo (dic). FullName;
SubNode. Tag = subNode. Name;
<Span style = "color: # ff0000;"> subNode </span>. <span style = "color: # ff0000;"> Nodes </span>. <span style = "color: # ff6666;"> Add </span> <span style = "color: # ff0000;"> (""); </span>
TNode. Nodes. Add (subNode );
 
}
String [] files = Directory. GetFiles (path );
Foreach (string aFile in files)
{
TreeNode subNode = new TreeNode (new FileInfo (aFile). Name );
SubNode. Tag = aFile;
SubNode. Name = subNode. Text;
TNode. Nodes. Add (subNode );
}
}
} </Span>

Pay attention to e. Node. Nodes. Clear (); and add an empty Node to the Node:
TNode. Nodes. Add ("DUMMY ");
And
SubNode. Nodes. Add ("");
These are all ways to add empty nodes to display the "plus sign "... Delete the file when it is officially loaded.

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.