Zoha Shobbar
Zoha Shobbar

Reputation: 528

how to change tree view node text after label edit?

I create a tree view in my form and added some nodes and there child. I enabled the label edit property and make the edit begin in after edit event I write a code to save the label text in my database and after that i disable the label edit. but after all of this I want to change the text of node that its label is edited. I write this code:

Private Sub TreeView1_NodeMouseDoubleClick() 
    TreeView1.LabelEdit = True
    TreeView1.SelectedNode.BeginEdit()
End Sub

 Private Sub TreeView1_AfterLabelEdit()
     Dim obj_customers As New Clas1_customers
     Dim Entity As New tblCustomers
     inputText = e.Label
     Entity.C_type= inputText 

     obj_customers .Update(Entity)

     TreeView1.LabelEdit = False
     e.Node.Text = e.Label & e.Node.Index
 End Sub

But the node text did not changed at last and only show the label in tree view. What can I do?

Upvotes: 0

Views: 7966

Answers (2)

Selcuk OZEN
Selcuk OZEN

Reputation: 36

I'd been looking at a solution to label edit problem with treeviews and came out with a workaround.

When you want to edit a label, Treeview creates a textbox and using user32.dll you can get the handle to the dynamically created textbox. You can declare functions and event handlers using this handler.

I preferred, creating an invisible textbox, positioning it as treeview would just do to edit the treenode, therefore using its all methods and event handlers.

Therefore treeview's labeledit properties set to false, just initiating visibility and positioning the textbox over selected node saved me.Below there is sample vb code,

    Private Sub treeview1_NodeMouseDoubleClick(sender As Object, e As TreeNodeMouseClickEventArgs) Handles treeview1.NodeMouseDoubleClick
        ShowLabelTextBox()
    End Sub
Private Sub ShowLabelTextBox()
    Dim x, y As Int32
    x = CInt(treeview1.Bounds.Location.X) + CInt(treeview1.SelectedNode.Bounds.Location.X)
    y = CInt(treeview1.Bounds.Location.Y) + CInt(treeview1.SelectedNode.Bounds.Location.Y)
    LabelTextBox.Text = treeview1.SelectedNode.Text
    LabelTextBox.SetBounds(x, y, LabelTextBox.Width, 8)
    LabelTextBox.Show()
    LabelTextBox.Focus()

    LabelTextBox.SelectAll()

End Sub

Private Sub LabelTextBox_KeyDown(sender As Object, e As KeyEventArgs) Handles LabelTextBox.KeyDown
    'User pressed enter, update treenode
    If e.KeyCode = Keys.Enter Then
            treeview1.SelectedNode.Text = LabelTextBox.Text
            treeview1.Focus()
'user pressed cancel, just leave node as it was
    ElseIf e.KeyCode = Keys.Escape Then
        treeview1.Focus()
    End If

End Sub

Private Sub LabelTextBox_LostFocus(sender As Object, e As EventArgs) Handles LabelTextBox.LostFocus
'User clicked anywhere, just leave everything as it was
    LabelTextBox.Hide()

End Sub

Hope it might help

Upvotes: 1

Alex v Pin
Alex v Pin

Reputation: 26

Private Sub m_pTreeView_AfterLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles m_pTreeView.AfterLabelEdit

    e.CancelEdit = True ' Switch OFF standard edit-end and set values manuell.

    If IsNothing(m_pTreeView.SelectedNode) Then Return ' Unknown error - impossible.
    If IsNothing(e.Label) Then Return ' Canceled from user.

    If 1 > e.Label.Length Then ' Set x.Text = F(x.Name)
        m_pTreeView.SelectedNode.Text = "NodeDefaultText_" + m_pTreeView.SelectedNode.Name
    else
        m_pTreeView.SelectedNode.Text = e.Label ' Same as by "standard edit-end"
    End If

End Sub

Upvotes: 1

Related Questions