Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,491 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,721 people online right now. Registration is fast and FREE... Join Now!




Updating changed TreeView Nodes

 
Reply to this topicStart new topic

Updating changed TreeView Nodes

SpiderSpartan
20 Feb, 2008 - 10:26 AM
Post #1

D.I.C Head
Group Icon

Joined: 6 Feb, 2008
Posts: 66



Thanked: 3 times
Dream Kudos: 25
My Contributions
OK, I've got this TreeView object set up and I've loaded it with values from an ArrayList of ArrayLists. When the user changes the text of the nodes in the TreeView I want to save that change back to the ArrayList. The problem is that my AfterLabelEdit function still sees the original text that was in the node.

CODE


private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
     Items[e.Node.Parent.Index][e.Node.Index] = e.Node.Text;
}



Say the node text was "SubItem0-1" and the user changes the text to "EDITED". This function is called after the editing is completed but the e.Node.Text still gives the original value. Is there any way to refresh the TreeView or something so that I can get the new text set by the user?
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Updating Changed TreeView Nodes
20 Feb, 2008 - 11:25 AM
Post #2

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,947



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Can you post the code that shows the actual modifying of the TreeView after the user edits it?
User is online!Profile CardPM
+Quote Post

SpiderSpartan
RE: Updating Changed TreeView Nodes
20 Feb, 2008 - 11:31 AM
Post #3

D.I.C Head
Group Icon

Joined: 6 Feb, 2008
Posts: 66



Thanked: 3 times
Dream Kudos: 25
My Contributions
QUOTE(jayman9 @ 20 Feb, 2008 - 12:25 PM) *

Can you post the code that shows the actual modifying of the TreeView after the user edits it?


I didn't know you needed to modify the TreeView in code. I thought that by setting the LabelEdit property to true the users could edit the TreeView directly in the object. The text does change in the display, but the code shown before still references the original text for some reason.
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Updating Changed TreeView Nodes
20 Feb, 2008 - 12:35 PM
Post #4

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,947



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
Actually that is my mistake, I've never used that property before.

The treeView1_AfterLabelEdit event is fired directly after the edit but before the change is committed to the TreeView.

You probably noticed that the messagebox shows up and then the treeview shows the change. So to answer your question, you can't get the value you want from inside the AfterLabelEdit event, because the TreeView does not commit the change until after it finishes executing this event.

You will have to get the value after the event has finished. Sorry I know that is not that helpful at this point.

I will look into it further and see if I can find an alternative solution for you.
User is online!Profile CardPM
+Quote Post

SpiderSpartan
RE: Updating Changed TreeView Nodes
20 Feb, 2008 - 01:37 PM
Post #5

D.I.C Head
Group Icon

Joined: 6 Feb, 2008
Posts: 66



Thanked: 3 times
Dream Kudos: 25
My Contributions
Thanks. I guess I'll just mark the nodes, have the user submit the TreeView, and then cycle through to check for any changes.
User is offlineProfile CardPM
+Quote Post

SpiderSpartan
RE: Updating Changed TreeView Nodes
27 Feb, 2008 - 12:47 PM
Post #6

D.I.C Head
Group Icon

Joined: 6 Feb, 2008
Posts: 66



Thanked: 3 times
Dream Kudos: 25
My Contributions
I finally stumbled upon how to do this. I added code to the BeforeLabelEdit and AfterLabelEdit code to save the original text stored in the node and compare it to the new text being input.

CODE

                string beforeEdit = "";
                string afterEdit = "";

        private void treeView3_BeforeLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            BeforeTreeEdit(treeView3, sender, e);
        }
        private void treeView3_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            AfterTreeEdit(treeView3, sender, e);
        }

        private void BeforeTreeEdit(TreeView Tree, object sender, NodeLabelEditEventArgs e)
        {
            // Determine whether the user has selected the top node. If so,
            // change the LabelEdit property to false so the user cannot
            // edit this label.
            if (e.Node == Tree.TopNode)
            {
                Tree.LabelEdit = false;
                MessageBox.Show("You are not allowed to edit the top node", "Illegal Node", MessageBoxButtons.OK);
            }
            // Allow the user to edit only the Value node
            else if (e.Node.Level < 2)
            {
                Tree.LabelEdit = false;
                MessageBox.Show("This node cannot be edited", "Illegal Node", MessageBoxButtons.OK);
            }
            Tree.LabelEdit = true;
            beforeEdit = e.Node.ToString();
        }
        private void AfterTreeEdit(TreeView Tree, object sender, NodeLabelEditEventArgs e)
        {
            afterEdit = e.Label;
            if (beforeEdit != afterEdit && afterEdit != null)
            {
                e.Node.BackColor = Color.Yellow;
            }
        }


I guess the node value/text/whatever doesn't get saved until after the AfterLabelEdit routine is finished, but the new value is present in the Node.Label object. Maybe this will help save someone else some time and hassle. biggrin.gif

This post has been edited by SpiderSpartan: 27 Feb, 2008 - 12:48 PM
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Updating Changed TreeView Nodes
27 Feb, 2008 - 02:52 PM
Post #7

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 6,947



Thanked: 42 times
Dream Kudos: 500
Expert In: C#, VB.NET, Java

My Contributions
I'm am glad that you were able to find a solution that worked for you. Thanks for sharing with us.
User is online!Profile CardPM
+Quote Post

richie5um
RE: Updating Changed TreeView Nodes
28 May, 2008 - 03:28 AM
Post #8

New D.I.C Head
*

Joined: 28 May, 2008
Posts: 1

Hi,

I may be wrong, but I think what you are after is something like this:

private void treeView_AfterLabelEdit( object sender, NodeLabelEditEventArgs e )
{
e.Node.EndEdit( false );

// Do stuff


The EndEdit(..) method terminates the edit, and preseves it in the node.Text.

Hope this helps,
RichS


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:41PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month