How can I check if a node in a treeview allready exist ?
-
I have a treeview where I add nodes. Before I add a new node to the treeview I want to check if a node with the same name allready exist, how can I do that in an easy way ? KRR
-
I have a treeview where I add nodes. Before I add a new node to the treeview I want to check if a node with the same name allready exist, how can I do that in an easy way ? KRR
if (!treeview1.Nodes.Contains(nodetoadd)) { //treeview1 doesn't contain nodetoadd } else { //treeview1 contains node nodetoadd }
#region signature my articles #endregion
-
if (!treeview1.Nodes.Contains(nodetoadd)) { //treeview1 doesn't contain nodetoadd } else { //treeview1 contains node nodetoadd }
#region signature my articles #endregion
That would not work, consider
TreeNode tn1 = new TreeNode("item1"); TreeNode tn2 = new TreeNode("item1"); treeView1.Nodes.Add(tn1); if(!treeView1.Nodes.Contains(tn2)){ treeView1.Nodes.Add(tn2); }else{ // dont add node }
treenode tn2 has the text as tn1, but it is not THE same treenode. but you could use the Name property and ContainsKey methodTreeNode tn1 = new TreeNode("item1"); tn1.Name = "item1"; TreeNode tn2 = new TreeNode("item1"); tn2.Name ="item1"; treeView1.Nodes.Add(tn1); if(!treeView1.Nodes.ContainsKey(tn2.Name )){ treeView1.Nodes.Add(tn2); }else{ MessageBox.Show("node exists"); }
If it' stuck, DO NOT pull harder!
-
That would not work, consider
TreeNode tn1 = new TreeNode("item1"); TreeNode tn2 = new TreeNode("item1"); treeView1.Nodes.Add(tn1); if(!treeView1.Nodes.Contains(tn2)){ treeView1.Nodes.Add(tn2); }else{ // dont add node }
treenode tn2 has the text as tn1, but it is not THE same treenode. but you could use the Name property and ContainsKey methodTreeNode tn1 = new TreeNode("item1"); tn1.Name = "item1"; TreeNode tn2 = new TreeNode("item1"); tn2.Name ="item1"; treeView1.Nodes.Add(tn1); if(!treeView1.Nodes.ContainsKey(tn2.Name )){ treeView1.Nodes.Add(tn2); }else{ MessageBox.Show("node exists"); }
If it' stuck, DO NOT pull harder!
Thanks. The last example was just the one I was looking for. It works great ! :-D KRR