471,618 Members | 1,235 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,618 software developers and data experts.

Expanding a treeview from a path for any number of levels.

I have finally developed some code that allows you to re populate a tree view
and re select the last node that was clicked. However, you have to hard code
it for the number of levels the tree view could possibly be expanded to. Any
ideas?

//The test program has a button to set the path and a button to execute the
path finding code.

//This is so that you can close the treeview back up to the root to ensure
the correct code is selected

string delimit = @"\";
char [] delimiter = delimit.ToCharArray();
string [] split = null;

split = path.Split( delimiter );

int CountUnderRoot = treeView1.Nodes[0].Nodes.Count ;
if( split.Length > 1 )
{
for( int i =0; i<CountUnderRoot; i++)
{
if(treeView1.Nodes[0].Nodes[i].Text == split[1].ToString())
{
Debug.WriteLine("yes");
int countUnderLevel1 = treeView1.Nodes[0].Nodes[i].Nodes.Count;
if( countUnderLevel1 > 0 && split.Length == 3 )
{
for( int j =0; j<countUnderLevel1; j++)
{
if(treeView1.Nodes[0].Nodes[i].Nodes[j].Text == split[2].ToString())
{
treeView1.Focus();
treeView1.SelectedNode = treeView1.Nodes[0].Nodes[i].Nodes[j];
break;
}
}
}

else
{
treeView1.Focus();
treeView1.SelectedNode = treeView1.Nodes[0].Nodes[i];
break;
}
}
}
}

else
{
treeView1.Focus();
treeView1.SelectedNode = treeView1.Nodes[0];
}
Feb 6 '06 #1
2 2265
After all that wonderful code, after a moment of inspiration(and guess work).
When i click the node i want i store the selected node in a private treeNode
variable.

I then close the tree View and say,

treeView1.SelectedNode = the priavte tree node variable.

The node seems to be selected correctly. I will now move it from my demo app
into the real thing. Fingers crossed.

"James L" wrote:
I have finally developed some code that allows you to re populate a tree view
and re select the last node that was clicked. However, you have to hard code
it for the number of levels the tree view could possibly be expanded to. Any
ideas?

//The test program has a button to set the path and a button to execute the
path finding code.

//This is so that you can close the treeview back up to the root to ensure
the correct code is selected

string delimit = @"\";
char [] delimiter = delimit.ToCharArray();
string [] split = null;

split = path.Split( delimiter );

int CountUnderRoot = treeView1.Nodes[0].Nodes.Count ;
if( split.Length > 1 )
{
for( int i =0; i<CountUnderRoot; i++)
{
if(treeView1.Nodes[0].Nodes[i].Text == split[1].ToString())
{
Debug.WriteLine("yes");
int countUnderLevel1 = treeView1.Nodes[0].Nodes[i].Nodes.Count;
if( countUnderLevel1 > 0 && split.Length == 3 )
{
for( int j =0; j<countUnderLevel1; j++)
{
if(treeView1.Nodes[0].Nodes[i].Nodes[j].Text == split[2].ToString())
{
treeView1.Focus();
treeView1.SelectedNode = treeView1.Nodes[0].Nodes[i].Nodes[j];
break;
}
}
}

else
{
treeView1.Focus();
treeView1.SelectedNode = treeView1.Nodes[0].Nodes[i];
break;
}
}
}
}

else
{
treeView1.Focus();
treeView1.SelectedNode = treeView1.Nodes[0];
}

Feb 6 '06 #2
How about something like:

private static void SelectNode(TreeView treeView, string indexPath) {
TreeNode node = null;
TreeNodeCollection nodes = treeView.Nodes;
foreach (string item in indexPath.Split('/')) {
node = nodes[int.Parse(item)];
nodes = node.Nodes;
}
if (node != null) {
treeView.SelectedNode = node;
node.EnsureVisible();
}
}
private static string GetIndexPath(TreeNode node) {
StringBuilder sb = new StringBuilder();
bool first = true;
while (node != null) {
if (first)
first = false;
else
sb.Append('/');
sb.Append(node.Index);
node = node.Parent;
}
return sb.ToString();
}
private void button1_Click(object sender, EventArgs e) {
SelectNode(treeView1, "0/1/0");
}

private void button2_Click(object sender, EventArgs e) {
MessageBox.Show(GetIndexPath(treeView1.SelectedNod e));
}

Marc
Feb 6 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Ferrari, Eduardo | last post: by
42 posts views Thread by lauren quantrell | last post: by
9 posts views Thread by Marina | last post: by
3 posts views Thread by Gerhard | last post: by
5 posts views Thread by Saabster | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.