标签编辑后如何更改树视图节点文本?

时间:2014-02-26 09:19:29

标签: vb.net treeview

我在表单中创建了一个树视图,并添加了一些节点和子节点。我启用了标签编辑属性并在编辑事件后开始编辑我编写了一个代码以将标签文本保存在我的数据库中,之后我禁用了标签编辑。但毕竟我想改变其标签编辑的节点文本。我写这段代码:

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

但节点文本最后没有改变,只在树视图中显示标签。我能做什么?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

我一直在寻找使用treeviews标记编辑问题的解决方案,并提出了解决方法。

当您想要编辑标签时,Treeview会创建一个文本框并使用user32.dll来获取动态创建的文本框的句柄。您可以使用此处理程序声明函数和事件处理程序。

我更喜欢创建一个不可见的文本框,将其定位为treeview只会编辑treenode,因此使用其所有方法和事件处理程序。

因此treeview的labeledit属性设置为false,只是启动可见性并将文本框定位在选定节点上保存了我。下面是示例vb代码,

    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

希望它可能会有所帮助