0% found this document useful (0 votes)
22 views

TreeView List of Directory

This code creates a Windows Forms tree view control that dynamically loads and displays the contents of a file system folder. When the form loads, it adds a root node for the C:\ drive and calls a method to populate its child nodes. This method gets the subdirectories of the given folder node and adds them as child nodes, with a placeholder node for future expansion. When a node is expanded that contains this placeholder, it clears the nodes and refills them by calling the population method again.

Uploaded by

dokeos
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

TreeView List of Directory

This code creates a Windows Forms tree view control that dynamically loads and displays the contents of a file system folder. When the form loads, it adds a root node for the C:\ drive and calls a method to populate its child nodes. This method gets the subdirectories of the given folder node and adds them as child nodes, with a placeholder node for future expansion. When a node is expanded that contains this placeholder, it clears the nodes and refills them by calling the population method again.

Uploaded by

dokeos
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

private void Form2_Load(object sender, EventArgs e)

{
TreeNode rootnode = new TreeNode(@"C:\");
treeView1.Nodes.Add(rootnode);
FillChildNodes(rootnode);
treeView1.Nodes[0].Expand();
}

void FillChildNodes(TreeNode node)


{
try
{
DirectoryInfo dirs = new DirectoryInfo(node.FullPath);
foreach (DirectoryInfo dir in dirs.GetDirectories())
{
TreeNode newnode = new TreeNode(dir.Name);
node.Nodes.Add(newnode);
newnode.Nodes.Add("*");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
if (e.Node.Nodes[0].Text == "*")
{
e.Node.Nodes.Clear();
FillChildNodes(e.Node);
}
}

You might also like