Solved

How to change the label of a treeview node

Posted on 2011-03-18
6
519 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Please explain: Aspect Oriented Programming 2 57
Hide Tab Page 3 20
Events in static methods 3 31
Saveas need to save a copy 16 14
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

11 Experts available now in Live!

Get 1:1 Help Now