1

enter image description here

I have a treeview node with 3 layer of hierarchy. Currently, the function for Add, Remove working. They only left is append the modified text into the selected node without remove the node before insert?

I have seen some of the question on this forum and from search engine result but the solutions look not like what I need.

Solutions that I found:-

  1. Get the selected node index, remove it, insert new to selected index node. On here

  2. Loop inside treeview node, to find and replace selected node string with new string. On here.

Why I can't follow above idea?

  1. If the selected node were remove first, so does the child will remove too?
  2. If there is more than 1 node that have same text, it must be replace on recursive loop.

What I have now are the index of Parent and Current Selected. So, it will be two index of hierarchy.

indxparent.Text = TreeView1.SelectedNode.Parent.Index.ToString() ?? "-";
indxchild.Text = TreeView1.SelectedNode.Index.ToString() ?? "-";

How to programming of amend new text to the selected node/ to the selected node index?

Working code use before Kempeth proposed his solutions:
Method 1:

string newdata = "This is new information data";
TreeView1.LabelEdit = true;
if (!TreeView1.SelectedNode.IsEditing)
{
    TreeView1.SelectedNode.BeginEdit();
    TreeView1.SelectedNode.Text = newdata;
    TreeView1.SelectedNode.EndEdit(false);
}  

Method 2:

string newdata = "This is new information data";
TreeNode node = new TreeNode(newdata);
TreeView1.SelectedNode.Parent.Nodes.RemoveAt(IndexOfSelectedNode);
TreeView1.SelectedNode.Parent.Nodes.Insert(IndexOfSelectedNode, node);
TreeView1.SelectedNode = node;

1 Answer 1

3

Have you tried simply setting the Text property of the SelectedNode?

TreeView1.SelectedNode.Text = TreeView1.SelectedNode.Text + " MAGIC!"
Sign up to request clarification or add additional context in comments.

1 Comment

Seriously man. I have try some like this. Not exactly like this but likely and failed. So, I've been stress finding the answer before I posted here for halfday of my workday. I even search using BeginEdit just not to remove which as I need. string newdata = "The new information data"; Treeview1.LabelEdit = true; if (!Treeview1.SelectedNode.IsEditing) { Treeview1.SelectedNode.BeginEdit(); Treeview1.SelectedNode.Text = newdata; Treeview1.SelectedNode.EndEdit(false); } But your solution, direct hit.. Thanks @Kempeth

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.