编辑目录树之一

本节内容可以参考CaseStudy目录下的文件:目录树管理.Table

假定并不想用表来存储目录结构,而是希望通过一个文本文件存储,并希望能够让修改并保存目录树结构。
实现这样的任务并不复杂,我们假定存储目录树结构的文件名“product.foxtr”,位于项目文件目录下。

设计的步骤为:

1、首先设计一个下图所示的窗口:

2、窗口的AfterLoad事件设置为:

Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim
fn As String = ProjectPath & "Product.foxtr"
If
Filesys.FileExists(fn) Then
    tr.Load(fn)

End
If

3、按下表所示设置各按钮的Click事件代码:

名称 代码
重命名 Dim tr As WinForm.TreeView
tr = e.Form.Controls(
"TreeView1")
tr.
Select()
tr.BeginEdit()
增加根节点 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()
增加子节点 Dim tr As WinForm.TreeView
Dim
nd As WinForm.TreeNode
tr = e.Form.Controls(
"TreeView1")
nd = tr.SelectedNode

If
nd IsNot Nothing Then
    nd = nd.Nodes.Add(
"新节点")
    tr.SelectedNode = nd
    tr.
Select
    tr.BeginEdit

End
If
插入节点 Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim
nd As WinForm.TreeNode = tr.SelectedNode
Dim
id As Integer = nd.index
If
nd.ParentNode Is Nothing Then
    nd = tr.Nodes.Insert(
"新节点",id)
Else

    nd = nd.ParentNode.Nodes.Insert(
"新节点",id)
End
If
tr.SelectedNode = nd
tr.
Select
tr.BeginEdit
上移 Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim
nd As WinForm.TreeNode = tr.SelectedNode
If
nd IsNot Nothing Then
    nd.MoveUp()

End
If
tr.
Select
下移 Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim
nd As WinForm.TreeNode = tr.SelectedNode
If
nd IsNot Nothing Then
    nd.MoveDown()

End
If
tr.
Select
左移 Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim
nd As WinForm.TreeNode = tr.SelectedNode
If
nd IsNot Nothing Then
    nd.MoveLeft()

End
If
tr.
Select
右移 Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim
nd As WinForm.TreeNode = tr.SelectedNode
If
nd IsNot Nothing Then
    nd.MoveRight()

End
If
tr.
Select
删除 Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim
nd As WinForm.TreeNode = tr.SelectedNode
If
nd IsNot Nothing Then
    nd.Delete

End
If
tr.
Select
保存 Dim tr As WinForm.TreeView = e.Form.Controls("TreeView1")
Dim
fn As String = ProjectPath & "Product.foxtr"
tr.Save(fn)

4、实际上,重命名节点修改的只是节点标题,而不是节点名称,所以我们还需要在AfterEditNode事件中设置代码,使得节点名称和标题保持一致:

e.Node.Name = e.NewText

5、下图是设计效果,用户可以并保存修改目录树结构:


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