简体   繁体   中英

C# Open listView Item from treeView item location

I'm needing a way to allow the files that are displayed in the ListView to be opened. The Items In the ListView are displayed from the TreeView . Take a look at my code below to see in more detail.

Code for this form

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace OSTP
{
    public partial class User1FileExplorer : Form
    {
        public User1FileExplorer()
        {
            InitializeComponent();
            PopulateTreeView();
            this.treeView1.NodeMouseClick +=
    new TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
        }

        private void User1FileExplorer_Load(object sender, EventArgs e)
        {

        }
        private void PopulateTreeView()
        {
            TreeNode rootNode;

            DirectoryInfo info = new DirectoryInfo(@"C:\Users\Oliver\Documents\.OSTP\User1\Files\Documents");
            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);
            }
        }

        private 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);
        }
    }
}

As you can see from my code I use a TreeView to select the directory for the files then the ListView to display the files inside of the directory.

I would like it so when the user double clicks the file in the ListView it opens. And when I say open I'm not meaning open a text file as such. So lets say the user double clicks text file 1 in the ListView , I would like it to show User1TextFile1.cs . This is because the text files are loaded into a text box.

I know this is a little complicated, so If I have missed anything please drop a comment.

Thanks.

UPDATE

http://pastebin.com/BgVdLavL

UPDATE2

When I add MessageBox.Show("" + lvHti);

To find out which ListViewItem was clicked or doubleclicked use the MouseClick or the MouseDoubleClick event.

Here you can either code simply:

ListViewItem lvItem = null;

if (listView1.SelectedItems.Count > 0)
    lvItem = listView1.SelectedItems[0];

provided the ListView has MultiSelect = false .

If you allow multiple selections you need to do a HitTest to find out where the user has clicked:

ListViewItem lvItem = null;

ListViewHitTestInfo lvHti = listView1.HitTest(e.Location);
if (lvHti.Item != null) lvItem = lvHti.Item;

Now you can access the Item and/or its SubItems to process the choice.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM