473,238 Members | 1,622 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,238 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 2364
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ferrari, Eduardo | last post by:
Hi Everybody I have a string that contains part of a directory structure that will be created in the future. I want to display this future tree on a treeview for the user see and then approve...
10
by: WJA | last post by:
I'm looking at building an application that needs to display 4 levels of hierarchical data. Initially the obvious choice for this would seem to be the treeview control. After searching this...
42
by: lauren quantrell | last post by:
So many postings on not to use the treeview control, but nothing recently. Is it safe to swim there yet with Access 2000-Access 2003?
9
by: Marina | last post by:
Hello! I have a database with some customers. Each one of them has 3 other customers under him etc. (something like an MLM system). Now, I want to create a treeview with this. Eg. C1 C11...
1
by: James L | last post by:
Hi, I have a treeview that has to be refreshed due to data changes. Is it possible to select the last node that they were viewing when the tree view was been populated once again? I have...
10
by: p3t3r | last post by:
I have a treeview sourced from a SiteMap. I want to use 2 different CSS styles for the root level nodes. The topmost root node should not have a top border, all the other root nodes should have a...
2
by: Tymbow | last post by:
I'm building a web application that is analogous to the Windows XP file explorer in function. The left column contains a TreeView, and the right column a DataGrid populated by selecting TreeView...
3
by: Gerhard | last post by:
I have the following code in an .net 2.0 project: <asp:Panel Runat="server" id="pnlNavigation" CssClass="pnlNavigation" Width="165px"> <asp:TreeView ID="TreeView1" runat="server"...
5
by: Saabster | last post by:
Hi all, I'm fairly new to VB.NET but have more experience with VB6 and earlier. I'm trying to create an application that will move files from one folder to another. Here is the scenario. I...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.