Solved

How to change the label of a treeview node

Posted on 2011-03-18
6
524 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
Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

617 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