Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How can I check if a node in a treeview allready exist ?

How can I check if a node in a treeview allready exist ?

Scheduled Pinned Locked Moved C#
question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kenth Ringsby
    wrote on last edited by
    #1

    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

    G 1 Reply Last reply
    0
    • K Kenth Ringsby

      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

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      if (!treeview1.Nodes.Contains(nodetoadd)) { //treeview1 doesn't contain nodetoadd } else { //treeview1 contains node nodetoadd }

      #region signature my articles #endregion

      C 1 Reply Last reply
      0
      • G Giorgi Dalakishvili

        if (!treeview1.Nodes.Contains(nodetoadd)) { //treeview1 doesn't contain nodetoadd } else { //treeview1 contains node nodetoadd }

        #region signature my articles #endregion

        C Offline
        C Offline
        c0ax_lx
        wrote on last edited by
        #3

        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 method TreeNode 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!

        K 1 Reply Last reply
        0
        • C c0ax_lx

          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 method TreeNode 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!

          K Offline
          K Offline
          Kenth Ringsby
          wrote on last edited by
          #4

          Thanks. The last example was just the one I was looking for. It works great ! :-D KRR

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups