Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
if i use rename();
but any node name is same of new name
then name is not change
Posted
Comments
ZurdoDev 10-Feb-14 13:53pm    
What?
BillWoodruff 11-Feb-14 0:51am    
The Windows Forms TreeView Control has no 'Rename method, although you can change the name of a Node at design-time, or run-time; is this WPF, or ASP.NET ? or ???? What TreeView Control is this ?

1 solution

I understand :) here is your solution.

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Test_TreeView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.Load += Form1_Load;
        }

        void Form1_Load(object sender, EventArgs e)
        {
            this.RenameNode("Node7", "Node6");

        }

        void RenameNode(string oldt, string newt)
        {
            // if new text exists return
            if (CheckNodeText(this.treeView1.TopNode, newt))
                return;
            // if node found, rename it
            TreeNode n = FindNode(this.treeView1.TopNode, oldt);
            if (n != null)
                n.Text = newt;
        }

        // check text if exists
        bool CheckNodeText(TreeNode node, string text)
        {
            if (node.Text == text)
                return true;
            for (int i = 0; i < node.Nodes.Count; i++)
            {
                if (CheckNodeText(node.Nodes[i], text))
                    return true;
            }
            return false;
        }

        // find the node having text
        TreeNode FindNode(TreeNode node, string text)
        {
            if (node.Text == text)
                return node;
            for (int i = 0; i < node.Nodes.Count; i++)
            {
                TreeNode n = FindNode(node.Nodes[i], text);
                if (n != null)
                    return n;
            }
            return null;
        }

    }
}
 
Share this answer
 
Comments
vyas4171 11-Feb-14 0:37am    
this code is not work because same name is also add...
Vedat Ozan Oner 11-Feb-14 3:45am    
have you tested it? I check 'Text' property which is visible on UI.
BillWoodruff 11-Feb-14 0:53am    
We don't have enough information from the OP about what they are doing, and what they really want, to answer them meaningfully, right now.

The OP implies they are dealing with the 'Name of TreeNodes, not their 'Text property.
Vedat Ozan Oner 11-Feb-14 3:47am    
ok, then change 'Text' into 'Name'.
vyas4171 11-Feb-14 11:45am    
ok

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900