Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Need a little help with the TreeView LabelEdit firing order

Posted on 2005-02-27
6
Medium Priority
?
327 Views
Last Modified: 2011-10-03
I have a TreeView in my Windows Forms application. There is a button on the menu that allows the user to edit the selected node. If you click on the button, it calls this routine:

    Private Sub EditNode()
        If Not (TreeView1.SelectedNode Is Nothing) And Not (TreeView1.SelectedNode.Parent Is Nothing) Then
            TreeView1.LabelEdit = True
            If Not TreeView1.SelectedNode.IsEditing Then
                TreeView1.SelectedNode.BeginEdit()
            End If
        Else
            MessageBox.Show("No tree node selected or selected node is a root node." & _
              Microsoft.VisualBasic.ControlChars.Cr & _
              "Editing of root nodes is not allowed.", "Invalid selection")
        End If
    End Sub

(I got that pretty much straight from MSDN - I dumbed it down just a bit in fact)

After the user edits the label, I want to call my function that persists it to XML. That function already exists and works, I just need to call it. So, logic dictates that I do this:

    Private Sub TreeView1_AfterLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles TreeView1.AfterLabelEdit
        TreeView1.SaveToXml("TreeView.xml")
    End Sub

Right? Well, here is the thing. The tree is being saved PRIOR to the edit. I put some MsgBox() popup in there just to trace how things were firing, and sure enough, AfterLabelEdit() is firing BEFORE BeginEdit(). Hey, I don't make this stuff up, I just work here :)

Anyway, I don't want the SaveToXML() to fire until AFTER the user edits the node text. Can anyone enlighten me as to what the heck is going on?
0
Comment
Question by:toddhd
  • 2
3 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13416199
Hi toddhd,

I can't reproduce your problem.  The AfterLabelEdit() event only seems to fire after a Label has been edited.  Do you perhaps have some code you forgot about in the BeforeLabelEdit() event?

    Private Sub TreeView1_BeforeLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles TreeView1.BeforeLabelEdit

    End Sub

~IM
0
 
LVL 8

Author Comment

by:toddhd
ID: 13416262
Thanks for responding IM - good thinking, but no, no BeforeLabelEdit() code is there. I'll keep looking around.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 13416282
Just for troubleshooting purposes, how about starting a new project with just a TreeView and a Button to see if it behaves differently?

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        Dim tn As TreeNode
        Dim n As Integer
        For i = 1 To 5
            n = n + 1
            TreeView1.Nodes.Add("Node" & n)
            For j = 1 To 3
                n = n + 1
                tn = TreeView1.Nodes(i - 1).Nodes.Add("Node" & n)
                For k = 1 To 3
                    n = n + 1
                    tn.Nodes.Add("Node" & n)
                Next
            Next
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        EditNode()
    End Sub

    Private Sub EditNode()
        If Not (TreeView1.SelectedNode Is Nothing) And Not (TreeView1.SelectedNode.Parent Is Nothing) Then
            TreeView1.LabelEdit = True
            If Not TreeView1.SelectedNode.IsEditing Then
                TreeView1.SelectedNode.BeginEdit()
            End If
        Else
            MessageBox.Show("No tree node selected or selected node is a root node." & _
              Microsoft.VisualBasic.ControlChars.Cr & _
              "Editing of root nodes is not allowed.", "Invalid selection")
        End If
    End Sub

    Private Sub TreeView1_AfterLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles TreeView1.AfterLabelEdit
        TreeView1.LabelEdit = False
        Debug.WriteLine("AfterLabelEdit")
    End Sub

    Private Sub TreeView1_BeforeLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles TreeView1.BeforeLabelEdit
        Debug.WriteLine("BeforeLabelEdit")
    End Sub

End Class
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

572 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