编辑目录树之二

本节的内容,可以参考CaseStudy目录下的文件:目录树与数据表.Table

本节的任务和上一节类似,不过目录树根据表生成,而且:

1、在目录树重命名节点,能够自动修改表中的相关数据。
2、在目录树删除节点,能够自动删除表中对应的行。
3、在目录树增加节点,能够自动在表中增加一行。

设计步骤:

1、首先设计一个上图所示的窗口。

2、窗口的AfterLoad事件代码为:

Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")
tr.BuildTree(
"表A", "大类|二类|三类"
)

3、“重命名”按钮的Click事件代码:

Dim tr As WinForm.TreeView
tr = e.Form.Controls(
"TreeView1")
tr.
Select()
tr.BeginEdit()

4、“增加根节点”按钮的Click事件代码:

Dim dr As DataRow = DataTables("表A").AddNew
dr(
"大类") = "新节点"
Dim
tr As WinForm.TreeView
Dim
nd As WinForm.TreeNode
tr = e.Form.Controls(
"TreeView1")
nd = tr.Nodes.Add(
"新节点")
tr.SelectedNode = nd
tr.
Select
()
tr.BeginEdit()

5、“增加子节点”按钮的Click事件代码:

Dim tr As WinForm.TreeView
Dim
nd As WinForm.TreeNode
tr = e.Form.Controls(
"TreeView1")
nd = tr.SelectedNode

If
nd IsNot Nothing Then
   
If nd.Level = 2 Then
        MessageBox.Show(
"最多允许三层节点!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information)
    Else
       
Dim dr As DataRow = DataTables("表A").AddNew
        If
nd.Level = 0
Then
            dr(
"大类") = nd.Text
            dr(
"二类") = "新节点"
       
ElseIf nd.Level = 1
            dr(
"大类") = nd.ParentNode.Text
            dr(
"二类") = nd.text
            dr(
"三类") = "新节点"
       
End If
        nd = nd.Nodes.Add(
"新节点")
        tr.SelectedNode = nd
        tr.
Select()
        tr.BeginEdit()

    End
If
End
If

6、“删除”按钮的Click事件代码:

Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim
nd As WinForm.TreeNode = tr.SelectedNode
If
nd IsNot Nothing Then
    Dim
pth() As String = nd.FullPath.Split("\")
    Select
Case nd.Level
    Case
0
       
DataTables("
A").DeleteFor("大类 = '" & pth(0) & "'")
    Case
1
       
DataTables("
A").DeleteFor("大类 = '" & pth(0) & "' And 二类 = '" & pth(1) & "'")
    Case
2
       
DataTables("
A").DeleteFor("大类 = '" & pth(0) & "' And 二类 = '" & pth(1) & "' And 三类 = '" & pth(2) & "'")
    End
Select
    nd.Delete()
End
If
tr.Select()

7、目录树AfterEditNode事件代码:

If e.NewText = "" Then
   
e.Cancel = True
    Return
End If
Dim
pth() As String = e.Node.FullPath.Split("\")
Select Case
e.node.Level
    Case
0
        DataTables(
"A").ReplaceFor("大类",e.NewText,"大类 = '" & pth(0) & "'")
    Case
1
        DataTables(
"A").ReplaceFor("二类",e.NewText,"大类 = '" & pth(0) & "' And 二类 = '" & pth(1) & "'")
    Case
2
        DataTables(
"A").ReplaceFor("三类",e.NewText,"大类 = '" & pth(0) & "' And 二类 = '" & pth(1) & "' And 三类 = '" & pth(2) & "'")
End Select

e
.Node.Name = e.NewText


本页地址:http://www.foxtable.com/webhelp/topics/2474.htm