By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,220 Members | 1,588 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,220 IT Pros & Developers. It's quick & easy.

find a TreeNode in a TreeView., how?

P: n/a
Hey

..NET 2.0

I'm trying to search a TreeView control for a specific TreeNode

The code below doesn't work because it only searches the the top level of
the nodes. I would like to program it so that it can find a TreeNode
anywhere in the TreeView... any suggestions on how to implement it ??

private bool PreRemoveTabPage(TabPage tab)
{
foreach (TreeNode n in TreeView1.Nodes)
{
if ((TabPage)n.Tag == tab)
{
n.Tag = null;
}
}
return true;
}
Oct 16 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Jeff,

I am brand new to C# and have been spending a lot of time researching
treeviews. I need to write a windows form to display SQL 2005 data in
a tree view. In my searching I came across these two URLs that may be
helpful to you:

Iterating Through All Nodes of a Windows Forms TreeView Control
http://msdn2.microsoft.com/en-us/lib...81(VS.71).aspx
How to: Iterate Through All Nodes of a Windows Forms TreeView Control
http://msdn2.microsoft.com/en-us/lib...z7(VS.80).aspx

Hope those sites are helpful.
My problem is I need to create a Windows form that will read a SQL
table and populate a treeview. I can connect to the DB, create the
dataadapter, populate a data set. The problem is how to use the
dataset to populate a treeview.

I have looked at a few examples but none use a dataset, or the data
structure was different and I could not modify to work with my data or
the examples were more than I needed and too complex for a beginner.
Can you suggest a URL, book or show me an example of code that simply
takes the data as shown and populates a treeview.

I would really appreciate the help.
I have a SQL Server 2005 table containing this data shown below.

Child Parent depth Hierachy
1 NULL 0 01
2 1 1 01.02
5 2 2 01.02.05
6 2 2 01.02.06
3 1 1 01.03
7 3 2 01.03.07
11 7 3 01.03.07.11
14 11 4 01.03.07.11.14
12 7 3 01.03.07.12
13 7 3 01.03.07.13
8 3 2 01.03.08
9 3 2 01.03.09
4 1 1 01.04
10 4 2 01.04.10
15 NULL 0 15
15 15 1 15.15
16 15 1 15.16
18 16 2 15.16.18
17 15 1 15.17

On Tue, 16 Oct 2007 21:37:17 +0200, "Jeff"
<it************@hotmail.com.NOSPAMwrote:
>Hey

.NET 2.0

I'm trying to search a TreeView control for a specific TreeNode

The code below doesn't work because it only searches the the top level of
the nodes. I would like to program it so that it can find a TreeNode
anywhere in the TreeView... any suggestions on how to implement it ??

private bool PreRemoveTabPage(TabPage tab)
{
foreach (TreeNode n in TreeView1.Nodes)
{
if ((TabPage)n.Tag == tab)
{
n.Tag = null;
}
}
return true;
}

Oct 16 '07 #2

P: n/a
Jeff wrote:
Hey

.NET 2.0

I'm trying to search a TreeView control for a specific TreeNode

The code below doesn't work because it only searches the the top level of
the nodes. I would like to program it so that it can find a TreeNode
anywhere in the TreeView... any suggestions on how to implement it ??
You need something that can walk the entire list. Here's a modified
sample of something that does this:
private static bool RemoveSpecificNode(
TreeNodeCollection tnc, string nodeName)
{
for (int i = 0; i < tnc.Count; i++)
{
if (tnc[i].Name == nodeName)
{
tnc.Remove(tnc[i]);
return true;
}
else if (tnc[i].Nodes.Count 0)
{
bool b = RemoveSpecificNode(tnc[i].Nodes, nodeName);
if (b)
return b;
}
}
return false;
}

Basically, just pass it the Nodes collection from your treeView object,
and the nodeName you want removed.

Chris.
Oct 16 '07 #3

P: n/a
Jeff wrote:
I'm trying to search a TreeView control for a specific TreeNode

The code below doesn't work because it only searches the the top level of
the nodes. I would like to program it so that it can find a TreeNode
anywhere in the TreeView... any suggestions on how to implement it ??
Well, IMHO the most obvious answer is to simply make your method recursive:

private bool PreRemoveTabPage(TabPage tab)
{
ResetTag(tab, TreeView1.Nodes);
return true;
}

private void ResetTag(TabPage tab, TreeNodeCollection nodes)
{
foreach (TreeNode n in nodes)
{
if ((TabPage)n.Tag == tab)
{
n.Tag = null;
}

if (n.Nodes != null)
{
ResetTag(tab, n.Nodes);
}
}
}

Alternatively, you could use the TreeNodeCollection.Find() method,
assuming you have some way of mapping the TabPage to a string that you
can use to look up the node. This will return an array of TreeNodes
that you can then just iterate through and reset individually as in your
original code (logically, it just flattens the tree to a single array
that contains only the items you want).

(BTW, I forget whether TreeNode.Nodes can ever be null or not. If it's
non-null even when the collection is empty, you could skip the test for
null protecting the recrusive call).

Pete
Oct 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.