Help with setting and expanding nodes in treeview
-
Hey everyone, I have a problem with seting and expanding nodes in a explorer clone program I am trying to make. The problems comes as when I am trying to open a folder in the right side of the explore bar (the right side is a TreeView and the left is a ListView). I can't find a method that will allow me to change the selected node in a treeView and also expand it. Please help. Part of my program's source code is here private void lv_ItemActivate(object sender, System.EventArgs e) { ListView lv = (ListView)sender; foreach (ListViewItem lvi in lv.SelectedItems) { string fullPath = tvw.SelectedNode.FullPath.ToString() + "\\" +lvi.Text.ToString(); MessageBox.Show(lvi.Text); DirectoryInfo di = new DirectoryInfo(fullPath); if ((di.Attributes & FileAttributes.Directory)!=0) { //here is what I have being attempting but to no avil because the selected node property is read only tvw.SelectedNode = new TreeNode(lvi.Text); tvw.SelectedNode.FullPath = fullPath; tvw.SelectedNode.Expand(); return; flow PS: Thanks for the help in advance, I am new to the forum and I hope I can contribute and learn a lot in here.
-
Hey everyone, I have a problem with seting and expanding nodes in a explorer clone program I am trying to make. The problems comes as when I am trying to open a folder in the right side of the explore bar (the right side is a TreeView and the left is a ListView). I can't find a method that will allow me to change the selected node in a treeView and also expand it. Please help. Part of my program's source code is here private void lv_ItemActivate(object sender, System.EventArgs e) { ListView lv = (ListView)sender; foreach (ListViewItem lvi in lv.SelectedItems) { string fullPath = tvw.SelectedNode.FullPath.ToString() + "\\" +lvi.Text.ToString(); MessageBox.Show(lvi.Text); DirectoryInfo di = new DirectoryInfo(fullPath); if ((di.Attributes & FileAttributes.Directory)!=0) { //here is what I have being attempting but to no avil because the selected node property is read only tvw.SelectedNode = new TreeNode(lvi.Text); tvw.SelectedNode.FullPath = fullPath; tvw.SelectedNode.Expand(); return; flow PS: Thanks for the help in advance, I am new to the forum and I hope I can contribute and learn a lot in here.
The
TreeView.SelectedNode
property is NOT read-only, butTreeNode.FullPath
is (which you're trying to set). All you need to do is recursively find yourTreeNode
(based on the path if you like) and then setTreeView.SelectedNode
to it. If you want to expand it, call theExpand
method on yourTreeNode
.Microsoft MVP, Visual C# My Articles
-
The
TreeView.SelectedNode
property is NOT read-only, butTreeNode.FullPath
is (which you're trying to set). All you need to do is recursively find yourTreeNode
(based on the path if you like) and then setTreeView.SelectedNode
to it. If you want to expand it, call theExpand
method on yourTreeNode
.Microsoft MVP, Visual C# My Articles
Hey thanks for the help, I have being thinking of using the Parent property to recurse through the tree node, but I what I don't understand is that after this I am still going to find a full path, and I wondering what to do with that. Thanks for the help.
-
Hey thanks for the help, I have being thinking of using the Parent property to recurse through the tree node, but I what I don't understand is that after this I am still going to find a full path, and I wondering what to do with that. Thanks for the help.
You don't recurse up using the
Parent
, you recurse through the children using theNodes
property. All children have only one parent, so there's not even a need to use recursion since a simple iterative loop will work just fine. To find a full path, just split the string on the separate character ("\" by default) and look for theTreeNode
with the given text like so:public TreeNode FindPath(string path)
{
if (path == null) throw new ArgumentNullException("path");
string[] names = path.Split(PathSeparator);
return FindPathHelper(Nodes, names, 0);
}
private TreeNode FindPathHelper(TreeNodeCollection nodes,
string[] names, int iteration)
{
if (nodes != null && names != null &&
iteration >= 0 && iteration < names.Length)
{
foreach (TreeNode node in nodes)
{
if (string.Compare(node.Text, names[iteration], true) == 0)
return node;
TreeNode child = FindPathHelper(node.Nodes, names, ++iteration);
if (child != null) return child;
}
}
return null;
}Microsoft MVP, Visual C# My Articles
-
You don't recurse up using the
Parent
, you recurse through the children using theNodes
property. All children have only one parent, so there's not even a need to use recursion since a simple iterative loop will work just fine. To find a full path, just split the string on the separate character ("\" by default) and look for theTreeNode
with the given text like so:public TreeNode FindPath(string path)
{
if (path == null) throw new ArgumentNullException("path");
string[] names = path.Split(PathSeparator);
return FindPathHelper(Nodes, names, 0);
}
private TreeNode FindPathHelper(TreeNodeCollection nodes,
string[] names, int iteration)
{
if (nodes != null && names != null &&
iteration >= 0 && iteration < names.Length)
{
foreach (TreeNode node in nodes)
{
if (string.Compare(node.Text, names[iteration], true) == 0)
return node;
TreeNode child = FindPathHelper(node.Nodes, names, ++iteration);
if (child != null) return child;
}
}
return null;
}Microsoft MVP, Visual C# My Articles
Hello Heath, I know this thread over 5 years old and I hope you're still subscribing to it. I'm also trying to expand the path to a specific node in a treeview (i.e. a node that has just been added by a user). I almost understand your code example. However my question is, where did you get the TreeNodeCollection for the "Nodes" parameter in the initial FindPathHelper call (within the FindPath function)? I don't see it declared anywhere. Thanks, Sacha
-
Hello Heath, I know this thread over 5 years old and I hope you're still subscribing to it. I'm also trying to expand the path to a specific node in a treeview (i.e. a node that has just been added by a user). I almost understand your code example. However my question is, where did you get the TreeNodeCollection for the "Nodes" parameter in the initial FindPathHelper call (within the FindPath function)? I don't see it declared anywhere. Thanks, Sacha
As mentioned, "you recurse through the children using the Nodes property". The
Nodes
property is onTreeView
class and on theTreeNode
class (which is used to recurse into subnodes).This posting is provided "AS IS" with no warranties, and confers no rights. Software Design Engineer Developer Division Customer Product-lifecycle Experience Microsoft [My Articles] [My Blog]