Windows Develop Bookmark and Share   
 index > Windows Forms General > How to determine if node exists
 

How to determine if node exists

Hello,
I managed to create a treeview, but I don't want to have two same nodes (names of nodes) in parent node.
How can i check if node with given name already exists?

Thanx, beezgetz
Beezgetz  Tuesday, May 08, 2007 12:42 PM
Hello Beezgetz,
the reason i had put an additional check for selectedNode == null is because @ times there wont be any node selected in the treeview & hence the SelectedNode property would return null.
and as about the else part its just that i have created a node instance & adding it to the selectedNodes Nodes property. on additional thing here is i am setting the name for the new treenode. the Nodes.Find & Nodes.ContainsKey method searches for nodes which have the Name property same as the Key specified for search. hence initializing it.

Now coming to the problem that you are encountering with duplicate entries is bcause you are not specifying the name for the nodes that you have created/added. explicitly specify the Name property of the node. in or case the same as the Text. that would work

Cheers

a.Binny  Wednesday, May 09, 2007 5:19 PM

Hi beezgetz -

The TreeNodeCollection contains a method called ContainsKey that returns true if a node already exists with the provided Name (not Text).

http://msdn2.microsoft.com/en-us/library/system.windows.forms.treenodecollection.containskey.aspx

Here is a quick snippet. HTH

Code Snippet

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

TreeView tv = new TreeView();

// false

MessageBox.Show(tv.Nodes.ContainsKey("test").ToString());

TreeNode tn = new TreeNode("text");

tn.Name = "test";

tv.Nodes.Add(tn);

// true

MessageBox.Show(tv.Nodes.ContainsKey("test").ToString());

// false

MessageBox.Show(tv.Nodes.ContainsKey("text").ToString());

}

}

mberseth  Tuesday, May 08, 2007 3:06 PM
Hi mberseth,

First, Thanks, I'm a begginer so help is welcomed...

I ran on new application and it worked, showing 3 message boxes...

But let's say that I have a textBox, from which I input nodes into selected parent by clicking button1. I am not quite able to put Your code into practice...

TreeNode a = treeView1.SelectedNode;
if (.....if node exists )
{
MessageBox.Show("Please Rename");
}
else
{
a.Nodes.Add(textBox1.Text);
}
Beezgetz  Tuesday, May 08, 2007 4:05 PM

I can think of 2 ways you can do this. Both seem a little clunky, but I am sure someone will reply if they know of a better solution ...

1. If you always set the Name property of the TreeNode to the same value as the Text property, you should be able to leverage the ContainsKey method on the TreeNodeCollection.

2. You could always iterate over the nodes in the collection and see if a node with the provided Text value already exists

#1:

TreeNode a = treeView1.SelectedNode;
if (a.Nodes.ContainsKey(textBox1.Text))
{
MessageBox.Show("Please Rename");
}
else
{

TreeNode tn = new TreeNode();

tn.Text = textBox1.Text;

tn.Name = textBox1.Text;

a.Nodes.Add(tn);
}

#2:

string newName = textBox1.Text;

bool doesNodeAlreadyExist = false;

foreach(TreeNode node in a.Nodes)

{

if(node.Text == newName)

{

doesNodeAlreadyExist= true;

break;

}

}

if(doesNodeAlreadyExist )

{

MessageBox.Show("Please Rename");

}

else

{

a.Nodes.Add(newName);

}

mberseth  Tuesday, May 08, 2007 5:32 PM
TreeNode selectedNode = treeView1.SelectedNode;
if (selectedNode == null) return;
string newKey = textBox1.Text;

if (selectedNode.Nodes.Find(newKey, true).Length > 0) {
MessageBox.Show("Node with '" + newKey + "' name already exists.");
} else {
TreeNode newNode = new TreeNode(newKey);
newNode.Name = newKey;
selectedNode.Nodes.Add(newNode);
}

  • Proposed As Answer byClint77 Friday, May 29, 2009 3:11 PM
  •  
a.Binny  Tuesday, May 08, 2007 5:35 PM
Hello .binny and mberseth,

@ mberseth,
I did not understand the 2nd one, so I concentrate on first... I don't understand, but it works.
I don't understand how come that I have to use tn.Text and nt.Name, but i guess i have to..

@ .binny,
I'm not quite shure what (selestedNode == nul) stands for, but I have an idea how Find works.
But again, I don't understand else{} statement, but it works.

Now, this don't solve my problem, as I hope it would. I populate treeView at the start (loadForm) with directories and files with code I found on:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=135792&SiteID=1
What happens is, that if node exists in parent at the beggining, Yours code does not work first time around. Only second time and furder on. I'll try to explain a little more.

If Solomon (parent node) has Dick, Sally, Tommy and Harry (4 children nodes) at start up, (with Yours code) I can add another Dick, Sally, Tommy and Harry. So now I have 8 children. But if I try adding again, I get MessageBox: "Pleast Rename, already exists", and it stays at 8 nodes.
So I guess I have to read children first or something... Bouth, ContainsKey or Find just ignore what is loaded at the beggining.

Thanx guys for so far, I hope You can help me a little bit more

Best Regards, beezgetz
Beezgetz  Wednesday, May 09, 2007 6:15 AM
Hello Beezgetz,
the reason i had put an additional check for selectedNode == null is because @ times there wont be any node selected in the treeview & hence the SelectedNode property would return null.
and as about the else part its just that i have created a node instance & adding it to the selectedNodes Nodes property. on additional thing here is i am setting the name for the new treenode. the Nodes.Find & Nodes.ContainsKey method searches for nodes which have the Name property same as the Key specified for search. hence initializing it.

Now coming to the problem that you are encountering with duplicate entries is bcause you are not specifying the name for the nodes that you have created/added. explicitly specify the Name property of the node. in or case the same as the Text. that would work

Cheers

a.Binny  Wednesday, May 09, 2007 5:19 PM
Hello .binny,

It took a bit of courage to add
node.Name = subDir.Name; and
node.Name = file.Name.Replace(".ddt", "");
but it works perfectly!!! Thanks!

Plus, thanks for the explaination

Best Regards, Beezgetz
Beezgetz  Thursday, May 10, 2007 6:24 AM

Ok, I have a similiar problem in vb.net that I can't figure out.

I'll give a quick code block I'm trying to use.

I'm looking at the 3 level node such as

-EventName1 (Tree Root)

|

----- EventFlight (child of EventName1)

|

----- TeamName (child of EventFlight)

|

----- PlayerName (Child of TeamName)

-EventName2 (Next Tree Root)

If tv.Nodes(0).Nodes(1).Nodes.ContainsKey("Team1") = True Then

'add PlayerName's

Else

tv.Nodes(0).Nodes(1).Nodes.Add("Team1")

'add PlayerNames

End If

when I loop through this, it always adds the "Team1" node plus the player names.

Any suggestions? I have looked at all the code from the above messages and tried to incorp the coding but it I get the same results as above.

Terry Fox  Wednesday, January 09, 2008 9:45 PM
Hello Terry,

As much I want to help you, I can not. I am strugeling with C#, and I know nothing about VB.

Best Regards, Beezgetz

Beezgetz  Thursday, January 10, 2008 3:04 PM
THANK YOU THANK YOU so much for this, I wasted an hour trying the code that the MSDN Library shows that didn't work.....

Clint77  Friday, May 29, 2009 3:12 PM

You can use google to search for other answers

Custom Search

More Threads

• String Spacing Before Printing
• [StreamReader] can't extract from a text file for the second time
• Refrence files
• Eject CD
• .Net Forms Control embedded in IE
• How do I make a control like color-choice control in MS Excel 2003?
• MethodNotFound when invoked from a forms control
• Making a dropdown menu
• FlowLayoutPanel very poor layout performance
• SVG