Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C#
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        
        PopulateTreeView();
       
        this.treeView1.NodeMouseClick +=
        new TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
    }
    //--------------------------------------------------------------------------------------------------------

    private void ListView_MouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
    {       
    }

    //-----------------------------------------------------------------------------------------------------------------------------------------------
    void treeView1_NodeMouseClick(object sender,
     TreeNodeMouseClickEventArgs e)
    {
        TreeNode newSelected = e.Node;
        listView1.Items.Clear();
        DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
        ListViewItem.ListViewSubItem[] subItems;
        ListViewItem item = null;

        foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
        {
            item = new ListViewItem(dir.Name, 0);
            subItems = new ListViewItem.ListViewSubItem[]
                      {new ListViewItem.ListViewSubItem(item, "Directory"),
               new ListViewItem.ListViewSubItem(item,
            dir.LastAccessTime.ToShortDateString())};
            item.SubItems.AddRange(subItems);
            listView1.Items.Add(item);
        }
        foreach (FileInfo file in nodeDirInfo.GetFiles())
        {
            item = new ListViewItem(file.Name, 1);
            subItems = new ListViewItem.ListViewSubItem[]
                      { new ListViewItem.ListViewSubItem(item, "File"),
               new ListViewItem.ListViewSubItem(item,
            file.LastAccessTime.ToShortDateString())};

            item.SubItems.AddRange(subItems);
            listView1.Items.Add(item);
        }

        listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
    }


    private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
    {
    }

    private void PopulateTreeView()
    {
        TreeNode rootNode;

        DirectoryInfo info = new DirectoryInfo(@"../..");
        if (info.Exists)
        {
            rootNode = new TreeNode(info.Name);
            rootNode.Tag = info;
            GetDirectories(info.GetDirectories(), rootNode);
            treeView1.Nodes.Add(rootNode);
        }
    }

    private void GetDirectories(DirectoryInfo[] subDirs,
TreeNode nodeToAddTo)
    {
        TreeNode aNode;
        DirectoryInfo[] subSubDirs;
        foreach (DirectoryInfo subDir in subDirs)
        {
            aNode = new TreeNode(subDir.Name, 0, 0);
            aNode.Tag = subDir;
            aNode.ImageKey = "folder";
            subSubDirs = subDir.GetDirectories();
            if (subSubDirs.Length != 0)
            {
                GetDirectories(subSubDirs, aNode);
            }
            nodeToAddTo.Nodes.Add(aNode);
        }
    }
    //-----------------------------------------------------------------------------------------------------------------------------------------------
}
}
Posted
Updated 16-Nov-15 23:42pm
v2
Comments
Richard MacCutchan 17-Nov-15 4:44am    
Create a ListView with the file names and add an event handler for the double-click event.
BillWoodruff 17-Nov-15 5:13am    
Posting a big heap of code does not help you here. Post code related to specific issues you describe. What have you tried so far ?

private void ListView_MouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e) ... what's this ? it's certainly not a valid ListView_MouseDoubleClick EventHandler ?

I think your goal here is to co-ordinate click activities between ListView and TreeView ... but you need to tell us that is the case.

Since a ListView (unless you use Groups with great effort) does not support hierarchic display like a TreeView, clarify exactly how you want the ListView to act on a double-click. Are you using a TreeView, or not ?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900