Solved

How to change the label of a treeview node

Posted on 2011-03-18
6
523 Views
Last Modified: 2013-12-17
Hi,

I have a treeView1 and a contextMenuStrip1 attached to it.
I have made a few nodes in the nodes property of the treeview.
I want if  the user selects a treeview node in the treeview
and selects the item "Edit Node" of the contextMenuStrip1
a inputbox appears where the user can enter a new name
for the node. My problem is how can i pass the string of the
inputbox to the label of the node that is selected.

        private void editNodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string NewText = "";
            if (InputBox.Show("Enter new Folder", "Folder:", ref NewText, validation) == DialogResult.OK)
            {
            }
        }

Who knows the answer and is willing to help me?

Greetings,

Peter Kiers
0
Comment
Question by:peterkiers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 13

Expert Comment

by:gamarrojgq
ID: 35167619
Hi,

You can use the SelectedNode property of the Treeview like this

        private void editNodeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string NewText = "";
            if (InputBox.Show("Enter new Folder", "Folder:", ref NewText, validation) == DialogResult.OK)
            {
                treeView1.SelectedNode().Text = NewText ;
            }
        }

Hope that helps
0
 
LVL 7

Expert Comment

by:Alex_W
ID: 35167640
Assign the new text (say from TextBox1) to the text property of the SelectedNode:

TreeView1.SelectedNode.Text = TextBox1.Text
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 35167830
Why not just use the BeginEdit() method of the TreeNode itself?
http://msdn.microsoft.com/en-us/library/system.windows.forms.treenode.beginedit(VS.80).aspx
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 1

Author Comment

by:peterkiers
ID: 35167846
Is this right:

        private void tvSampleRightClickEdit(object sender, System.EventArgs e)
        {
            Hourglass(true);
            try
            {
                string NewText = "";
                TreeNode node = tvNotes.SelectedNode;
                if (node == null) { return; }
                if (node == tvNotes.Nodes[0]) { return; }
                if (InputBox.Show("Enter new Folder", "Folder:", ref NewText, validation) == DialogResult.OK)
                {
                    tvNotes.SelectedNode.Text = NewText; <========
                }
            }
            catch (Exception err) { MessageBox.Show(err.Message); }
           finally { Hourglass(false); }
        }

The line marked with an arrow I try to do this:

node.SelectedNode.Text = NewText;

But I get an error...

P.
0
 
LVL 13

Accepted Solution

by:
gamarrojgq earned 500 total points
ID: 35167867
Hi,

when you do this

TreeNode node = tvNotes.SelectedNode;

you are assigning tvNotes.SelectedNode to the varialbe Node, then you just have to do this

node.Text = NewText;
0
 
LVL 1

Author Comment

by:peterkiers
ID: 35167918
Thanks for everything.

Peter Kiers
0

Featured Post

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question