Solved

How to change the label of a treeview node

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

828 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