Solved

How to change the label of a treeview node

Posted on 2011-03-18
6
520 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
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 85

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now