二、增加、删除树节点
单纯在Treeview 上增加、删除、修改节点只需用Nodes属性的Add、 Remove、等方法即可,值得注意的地方是VS.NET中Treeview的Nodes集合与VS6.0中的区别,VS6.0中的是一个大的集合,而VS.NET中的是分层的每个Node下都有Nodes属性。增加、删除、修改树节点时与VS6.0相比有很大差别,特别是删除时。
单纯在Treeview 上增加、删除、修改节点只需用Nodes属性的Add、 Remove、等方法即可,值得注意的地方是VS.NET中Treeview的Nodes集合与VS6.0中的区别,VS6.0中的是一个大的集合,而VS.NET中的是分层的每个Node下都有Nodes属性。增加、删除、修改树节点时与VS6.0相比有很大差别,特别是删除时。
Private Sub ButAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButAdd.Click’在选定的节点下添加子节点 Dim tmpNd As New TreeNode(), NdSel As TreeNode tmpNd.ID = GetNewId() NdSel = TreeView1.GetNodeFromIndex(TreeView1.SelectedNodeIndex)’选中的节点 tmpNd.Text = "新节点" NdSel.Nodes.Add(tmpNd) Dim myRow As DataRow myRow = ds.Tables("tree").NewRow() myRow("NODE_NAME") = tmpNd.ID myRow("NODE_DESCRIPT") = "新节点" & tmpNd.ID & "_" & NdSel.ID myRow("PARENT_NAME") = NdSel.ID ds.Tables("tree").Rows.Add(myRow) End Sub Private Sub ButDele_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButDele.Click’删除选中的节点 Dim idx As String = TreeView1.SelectedNodeIndex() GetNdCol(idx).Remove(TreeView1.GetNodeFromIndex(idx)) Dim dv As New DataView(), recNo As Integer dv.Table = ds.Tables("tree") dv.RowFilter= "NODEID=" & NdId dv.Delete(0) End Sub Private Function GetNdCol(ByVal idx As String) As TreeNodeCollection ‘获得选中节点的父节点的Nodes集合 Dim cnt As Integer, i As Integer Dim tmpNds As TreeNodeCollection Dim idxs() As String idxs = Split(idx, ".") cnt = UBound(idxs) If cnt = 0 Then tmpNds = TreeView1.Nodes Else tmpNds = TreeView1.Nodes(CInt(idxs(0))).Nodes For i = 1 To cnt - 1 tmpNds = tmpNds(CInt(idxs(i))).Nodes Next End If Return tmpNds End Function |