树状图 TreeView控件--添加/删除节点(亲自实践)
来源:互联网 发布:数据分析师证书被取消 编辑:程序博客网 时间:2024/05/16 04:00
新增节点
'设定树状图中节点
Dim node As TreeNode
'Method 1: straightforward adding of nodes
With Me.TreeView1.Nodes
'add text
.Add("AddByText ")
'since with..end with is used: read TreeView1.Nodes.Add ....
'every add method returns the newly created node. You can use
'this concept set the result to a variable or to directly add
'a childnode:
.Add("AddByText2 ").Nodes.Add("ChildOfAddByText ")
'this, you can take as far as you want
.Add("AddByText3 ").Nodes.Add("ChildOfAddByText ").Nodes.Add("Another child ")
'--
node = .Add("AddByText, Attach To Variable ")
node.Nodes.Add("Child one ")
node.Nodes.Add("Child two ")
' --
With .Add("AddByText Use WithTo Add ChildNodes ").Nodes
.Add("Child 1 ")
.Add("Child 2 ")
.Add("Child 3 ").Nodes.Add("Subchild 1 ")
End With
End With
复制节点
'设定树状图中节点
Dim node As TreeNode
'Method 2: adding by node
'Like virtually every .Net method you can directly assign an object:
Me.TreeView1.Nodes.Add(New TreeNode("AddByNode1 "))
Me.TreeView1.Nodes.Add("AddByNode2 ").Nodes.Add("test1")
'check out the overloading possibilities of using New()
'Another advantage of this method is that you can add a complete branch.
'(node is already declared as TreeNode above)
node = New TreeNode("MainNodeToAdd ")
node.Nodes.Add("Child 1 ")
node.Nodes.Add("Child 2 ")
'you can for instance add this newly created node to all main branches:
Dim enumNode As TreeNode
For Each enumNode In TreeView1.Nodes
enumNode.Nodes.Add(node.Clone) ' <- the clone() method is needed
Next
说明:
红色字体部分,是将某个节点复制到指定节点之下.
注意,由于for循环的对象是TreeView1.Nodes,而节点"test1"的隶属关系是"AddByNode2".Nodes
所以节点"test1"没有被复制
追加节点
''Adding will always add the the node at the end of the collection.
''Of course you can also insert at a specified location:
Me.TreeView1.Nodes.Insert(2, New TreeNode("I am inserted at the 3th position "))
说明:
insert()函数中,第一个参数是从0 开始
所以上面方法三中第一个参数设定是2,而结果显示该节点插入位置为第三项
删除节点及其下所有内容
'设定树状图中节点
Dim node As TreeNode
''removing is done much in the same way:
TreeView1.Nodes.Add("I need to be removed ").Nodes.Add("and all children too ")
TreeView1.Nodes(0).Name = "test"
node = TreeView1.Nodes("test")
node.Remove()
说明:
注意,使用TreeView1.Nodes()定位节点的时候,有2种方法,
第一种,参数是Index,从0开始
第二种,参数是name,而不是text.
上例中如果写作TreeView1.Nodes("I need to be removed "),则会系统报错,Nodes返回值为Nothing
删除前:
删除后:
删除节点其下的所有内容(该节点保留)
'设定树状图中节点
Dim node As TreeNode
''removing is done much in the same way:
TreeView1.Nodes.Add("I need to be removed ").Nodes.Add("and all children too ")
TreeView1.Nodes(0).Name = "test"
node = TreeView1.Nodes("test")
node.Nodes.Clear()
说明:
clear()方法清除的是指定节点下所属的内容,而该指定节点不受影响
删除前:
删除后:
展开/关闭节点
'the behaviour of the treenode can be controlled completely in code.
'you can make it expand
TreeView1.Nodes(0).Expand()
''and retract again
TreeView1.Nodes(0).Collapse()
- 树状图 TreeView控件--添加/删除节点(亲自实践)
- 树状图 TreeView控件--访问控件的所有节点
- TreeView 添加,删除,更改 节点
- treeview节点的添加和删除
- 浅析动态添加、删除、修改TreeView节点
- treeView中单击右键添加/删除节点
- 给TreeView控件添加节点一例
- 递归为TreeView控件添加节点
- C# treeview控件部分节点添加checkbox
- C# treeview控件部分节点添加checkbox
- ASP.net 中关于控件Treeview的操作------ 节点查找、删除和添加
- RadTreeView控件添加删除节点
- 树状图 TreeView控件的使用
- javascript实现treeview无刷新添加和删除节点
- ASP.NET - 如何:添加或删除 TreeView 节点元素
- asp.net treeview数据库绑定 (节点添加 删除 修改)
- asp.net treeview数据库绑定 (节点添加 删除 修改)
- C# treeview右键添加,删除,重名字树节点
- 数据结构C语言实现系列——单链表
- asp.net的<% %>总结
- 树状图 TreeView控件--访问控件的所有节点
- Java常用文件目录处理代码集
- flex日期比较
- 树状图 TreeView控件--添加/删除节点(亲自实践)
- Androidpn的框架浅析
- .NET水晶报表简单使用
- 思科的云战略:强制与贪婪
- XML Schema使用参考手册
- 省略符形参
- 浏览器扩展反编译工具
- Windows Socket五种I/O模型——代码全攻略
- The selected wizard could not be started.Plug-in org.tigris.subversion.subclipse.ui was unable to l