473,804 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

find a TreeNode in a TreeView., how?

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 PreRemoveTabPag e(TabPage tab)
{
foreach (TreeNode n in TreeView1.Nodes )
{
if ((TabPage)n.Tag == tab)
{
n.Tag = null;
}
}
return true;
}
Oct 16 '07 #1
3 13815
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.NO SPAMwrote:
>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 PreRemoveTabPag e(TabPage tab)
{
foreach (TreeNode n in TreeView1.Nodes )
{
if ((TabPage)n.Tag == tab)
{
n.Tag = null;
}
}
return true;
}

Oct 16 '07 #2
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 RemoveSpecificN ode(
TreeNodeCollect ion 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 = RemoveSpecificN ode(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
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 PreRemoveTabPag e(TabPage tab)
{
ResetTag(tab, TreeView1.Nodes );
return true;
}

private void ResetTag(TabPag e tab, TreeNodeCollect ion 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 TreeNodeCollect ion.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5735
by: Dean L. Howen | last post by:
Hi, I want to add some attributes to TreeNode, so I create a new class MyNode that inheritance from System.Windows.Forms.TreeNode, I want to TreeView use MyNode instead of TreeNode, so I can manipulate with new attributes How can? Please give me some ideas, Thanks. Because the TreeNode doesn't have Value Property which contents value behind
9
12727
by: Mark Allison | last post by:
Hi there, I'm still very much a newbie to C#, and would like to derive from TreeNode to extend the TreeNode class. Here's test program: http://www.vkarlsen.no/pastebin/default.asp?id=4090 I get an error on line 0077 saying: "An unhandled exception of type 'System.InvalidCastException' occurred
6
3733
by: Craig Lister | last post by:
I have a treeview with a Root, a Contact Type, and a Contact. Like MSN Messenger. Is there a way that I can say something like if treenode.tag is classContact then string Surname = treenode.tag.surname And somehow, each time, I create the node, I can assign the object of that classContact to that node ?
3
4044
by: tanya foster | last post by:
Hello, I am re-writing a visual basic .net application(visual studio 2003) in an asp.net application(visual studio 2005). The vb.net application relied on a treeview and hence, treenodes. The treeview and treenode class in asp.net is limited compared to vb.net's. In vb.net, the treenode had a nextnode property which was equivalent to finding the next sibling of the treenode. asp.net's treenode has a child property and a parent property....
0
12794
by: jiing | last post by:
Hi all, I want to use sting(the same as Node.Text) to judge if a node exists in TreeView. I've tried several ways, but seems all failed. could anybody help me? Thanks in advance. //My TreeView is defined to has depth =3 only.
9
3714
by: vincent90152900 | last post by:
How to pop up different text base on the selected TreeNode? I want to pop up different Text base on the selected TreeNode of the TreeView component. So, I create a TreeView and a ModalPopupExtender. Following is my codes. <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <asp:TreeView ID="TreeView1" runat="server" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged"> <Nodes>...
0
3315
by: divya1949 | last post by:
Create a windows c# application which will Read a xml file and populate nodes in the treeview. 1 On selection of treenode display the child nodes of that node in listview control 2. Provide following view properties to listview, through View menu a. Tile b. Icon
1
9910
by: JR | last post by:
Hi, I need a routine/finction that finds a text, starting at the selected node, where a given text is in. say i have some nodes with 1 'sonday' and another with 'son' and I look for 'so' it will first find 'sonday' and the next time 'son'. ones it have found everything with the look parameter it restart at the top. Jan
1
3279
by: AAaron123 | last post by:
If you see this posted twice - sorry. My news reader showed my first post as "No Longer Available" I have the following in a .css file. The treeNodes behave as if they were "a" elements. I can't get them to not respond to the mouse!
0
9594
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10350
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10351
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9174
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7638
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6866
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3002
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.