树的生成

29 篇文章 0 订阅
Technorati 标签: treeview, 递归生成树, imagelist

版权声明:可以任意转载,转载时请务必以超链接形式标明如下文章原始出处和作者信息及本声明

作者:xixi

出处:http://blog.csdn.net/slowgrace/archive/2008/12/09/3483119.aspx 

一、用ADD方法添加节点

生成树其实很简单,只要用Treeview 控件的Add方法一个个地把树的节点加上就行。Add方法的参考信息如下:

[功能] 在Treeview 控件的Nodes 集合中添加一个Node对象。

[语法] object.Add(relative, relationship, key, text, image, selectedimage)

[返回] 返回新添加的Node对象。

[说明]

  1. 参数Object是必需的,为对象表达式,该对象必须是MSComctlLib.TreeView类的对象哦。
  2. 参数Relative是可选的,代表已存在的Node对象的索引号或键值。如果在relative中没有被命名的Node对象,则新节点被放在节点顶层的最后位置。
  3. 参数relationship是可选的,代表新节点与已存在的节点间的关系,指定Node对象的相对位置。relationship的设置值是:
    1. 0——tvwFirst 首节点,该Node和在relative中被命名的节点位于同一层,并位于所有同层节点之前。
    2. 1——tvwLast 最后的节点,该Node和在relative中被命名的节点位于同一层,并位于所有同层节点之后。任何连续地添加的节点可能位于最后添加的节点之后。
    3. 2——tvwNext(缺省),下一个节点,该Node位于在relative中被命名的节点之后。
    4. 3——tvwPrevious,前一个节点,该Node位于在relative中被命名的节点之前。
    5. 4——tvwChild(缺省),子节点。该Node为在relative中被命名的节点的子节点。
  4. 参数key是可选的,唯一的字符串,可用于用Item 方法检索Node。
  5. 参数text是必需的,在Node中出现的字符串。
  6. 参数image是可选的,代表一个图像或在ImageList控件中图象的索引。注意,这个值必须是索引值,一个整数;而不能是带双引号的别名,我曾经因为用这个别名导致程序调不出来,折腾了个多小时!
  7. 参数selectedimage是可选的,代表一个图像或在ImageList控件中图象的索引,在Node被选中时显示。

二、给节点附着有意义的信息

node对象有一些属性,可以用来存储与它相关的信息:

属性说明
.text节点的名称
.indexUnique number assigned to a node by VBA,貌似是动态变化的
.keyUnique value assigned by developer, must start with a letter
.tagstring类型,存嘛开发者自己定

 

示例:添加一个节点,并且设置它的相关信息

tt

 

三、递归地添加节点

现在我们学会了添加节点,但是,如果我们要维护的层次化信息有数千个节点、十好几个层次,这么一个个地添那得累S。一个聪明的办法是把节点信息都存在表里,表里的每条记录指明自己的father node是谁,然后用程序递归地从这个表里读信息添加节点。节点表包含的字段如下:

tt

 

递归的过程如下:

生成孩子节点(node)

示例的代码如下,点击可以放大哦:

tt

 

 

注意,这里只写了生成child的过程,而最初的根节点还是要单独写代码来添加,添加后再调用这个根节点的NODE_ADDCHILD过程即可。

四、第0步,添加TREEVIEW控件

要让以上代码能run,你得首先把treeview控件加到窗体上,这是一切的根本,第一步之前的第0步。具体方法如下:

  1.  TREEVIEW控件放到窗体上:
    1. Start a new blank form, insert the treeview control by choosing Insert/ActiveX Control. . . from the main menu, as shown in Figure 10-8. The figure displays the ActiveX Control selection dialog box. For this example, the Treeview control is from Visual Basic 6, SP 6.
    2. Select the desired control from the ActiveX Control dialog box, and it will be added to the form. Depending on the type of control selected, it may need to be moved, resized, or have its properties modified.
    3. It is important to note that an ActiveX control has properties that are displayed in the standard properties window, but a control can also possess another set of custom properties that were added by the control creator. In Figure 10-9, both the standard and custom property windows are displayed.
  2. 设置imagelist: In order for a Treeview control to be operational, you must create an ImageList control. The ImageList control is a storage area for pictures that the Treeview uses when it displays the data.
    1. 把imagelist控件放到窗体上:To add an ImageList control to the form, use the ActiveX Controls selection dialog box and select the Microsoft ImageList control.
    2. 在imagelist中添加图标文件:
      1. 打开imagelist属性对话框:Right-click on the inserted ImageList control, select ImageListCtrl object from the submenu, then select the Properties choice. These steps open the ImageListCtrl Properties dialog box, which is where the images and their properties are defined.
      2. 设置图标大小:On the General tab, first select the size of the icons, as this value cannot be changed after images are inserted.
      3. 添加图标文件:The Images tab contains the inventory of icons. To add an image to the inventory, click on the Insert Picture button. This opens the standard Windows File Open dialog box to allow the user to browse for image files. Each image in the list has an Index, Key, and Tag property.
  3. 把imagelist和treeview控件关联起来:After defining the icons in the ImageList, the final setup step is to set the ImageList property of the Treeview control to the name of the ImageList control. To perform this step, right-click the Treeview control, choose TreeCtrl object, then Properties, and finally select the ImageList control name for the ImageList property.

tt

tt

 

 

 

五、生成树完整过程回放 

  1. 在窗体设计视图做如下操作:
    1. 在窗体中放上treeview控件和imagelist控件;
    2. 给imagelist控件加上图标,并把treeview控件和imagelist控件关联起来;
  2. 在表设计视图,建立node表并录入一些数据。
  3. 在代码窗口,完成递归生成树节点的代码。

参考文献:

  1. http://fanjy.blog.excelhome.net/user1/fanjy/archives/2007/910.html
  2. Patricia Cardoza, Teresa Hennig, Graham Seach, Armen Stein, "Access 2003 VBA Programmer’s Reference", Published by Wiley Publishing, Inc., Indianapolis, Indiana, 2004

更多树类文章

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值