How to find treeview directory node when activate a listview directory?
Hi,
I am creating a custom directory browser with TreeView/ListView.
the ListView contains Directories & Files.
When the user double clicks(I handle this with activate Item) on an Item in the list view,
the code has to find the item that was activated, determine whether it was a directory or a file.
1)When it is a file it must Start the file with the associated software.
2)When it is a directory and the user enters that directory it must also find that directory in the tree view, and select the corresponding node
How can I find that directory in the tree view, and select the corresponding node?
This is my Activate Item code:
private void ProjectListView_ItemActivate(object sender, System.EventArgs e)
{
//ItemInfo when Selecting a ListView Item
DirectoryInfo ItemInfo = new DirectoryInfo(sProjectsPath + "\\" + ProjectTreeView.SelectedNode.FullPath + "\\" + ProjectListView.SelectedItems[0].Text);
// Show Directory Path of ItemInfo in StatusBar
StatusLabel1.Text = (ItemInfo.ToString());
//If the ItemInfo is a file then run the file
if (ItemInfo.Exists != true)
{
try
{
Process.Start(ItemInfo.ToString());
}
catch (Exception Exc) { MessageBox.Show(Exc.ToString()); }
}
//If the ItemInfo is a folder then open the folder in ListView
else
{
try
{
ProjectListView.BeginUpdate();
ProjectListView.Items.Clear();
ListViewItem.ListViewSubItem[] subItems;
ListViewItem item = null;
//ListView items are folders
foreach (DirectoryInfo dir in ItemInfo.GetDirectories())
{
item = new ListViewItem(dir.Name, 1);
subItems = new ListViewItem.ListViewSubItem[]
{ new ListViewItem.ListViewSubItem(item, ""),
new ListViewItem.ListViewSubItem(item, "File Folder"),
new ListViewItem.ListViewSubItem(item, dir.LastAccessTime.ToShortDateString()) };
item.SubItems.AddRange(subItems);
ProjectListView.Items.Add(item);
}
//ListView items are files
foreach (FileInfo file in ItemInfo.GetFiles())
{
item = new ListViewItem(file.Name, 2);
subItems = new ListViewItem.ListViewSubItem[]
{new ListViewItem.ListViewSubItem(item, Convert.ToString(file.Length / 1024) + " kB"),
new ListViewItem.ListViewSubItem(item, "File"),
new ListViewItem.ListViewSubItem(item, file.LastAccessTime.ToShortDateString())};
item.SubItems.AddRange(subItems);
ProjectListView.Items.Add(item);
}
ProjectListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
ProjectListView.EndUpdate();
}
catch (Exception Exc) { MessageBox.Show(Exc.ToString()); }
}
}