?
Solved

Need a little help with the TreeView LabelEdit firing order

Posted on 2005-02-27
6
Medium Priority
?
296 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
6 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 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