为PB 的TreeView实现同步选择

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

为PB 的TreeView实现同步选择

广州中山大学信息管理系2001级 梁冠超(510275)

TreeView控件能够清晰地表示层次关系,因而赢得了众多程序员的喜爱。在TreeView编程中,常见的一个问题是同步选择(即选择一个节点时,同时选择该节点的所有子节点;不选一个节点,同时去除该节点的祖先节点选择标志)。

要实现这种效果,本来不难,只需要在检测到节点选择状态变化时,遍历节点的祖先或者后代节点进行同步即可。但是PB并没有提供检测节点选择状态变化的事件。怎么办呢?

让我们来看看PB中TreeView节点选择状态的表示。当TreeView的CheckBoxes属性为True时,每个节点包含一个复选框。选中复选框时,StatePictureIndex属性为2,未选中则为1。当选择某个节点时,首先触发TreeView的Clicked事件,处理完Clicked事件后再对StatePictureIndex进行设置。显然,如果我们能够将Clicked事件前后节点的StatePictureIndex属性值进行比较,我们就可以判断节点的选择状态是否发生了变化。按照这个思路,疏理PB的事件模型,发现采用Post的调用事件方法,可以实现将一个事件加入控件消息序列,在处理完当前事件后再对新加入的事件进行处理。至此,我们得到了如下的解决方案:

1. 为TreeView控件添加一个用户事件ue_synchronizechildren(long handle, integer state),代码如下:

long childitem

treeviewitem tvitem

getitem(handle, tvitem)

tvitem.statepictureindex=state

setitem(handle, tvitem)

childitem=this.finditem(ChildTreeItem!, handle)

do while(childitem<>-1)

this.Event ue_synchronizechildren(childitem, state) //递归遍历后代结点

childitem=this.finditem(NextTreeItem!, childitem)

loop

添加一个用户事件ue_synchronizeparent(long handle, integer state),如下:

long parentitem

treeviewitem tvitem

getitem(handle, tvitem)

tvitem.statepictureindex=state

setitem(handle, tvitem)

parentitem=this.finditem(ParentTreeItem!, handle)

if parentitem<>-1 then

this.Event ue_synchronizeparent(parentitem, state)

end if

2. 添加一个用户事件ue_statechanged(long handle, integer prevstate)检测节点的选择状态, 如果发生了变化则调用ue_synchronizechildren同步后代节点,并根据需要通过ue_synchronizeparent同步祖先节点。代码如下:

treeviewitem tvitem

getitem(handle, tvitem)

if tvitem.statepictureindex=prevstate then

return

else

this.Event ue_synchronizechildren(handle, tvitem.statepictureindex)

if tvitem.statepictureindex=1 then //如需实现文末提及的功能,可在此处添加代码。

this.Event ue_synchronizeparent(handle, tvitem.statepictureindex)

end if

end if

3. 在Clicked事件中,添加如下调用:

treeviewitem tvitem

getitem(handle, tvitem)

post event ue_statechanged(handle, tvitem.statepictureindex)

为了简单起见,上面的代码并没有考虑当选上一个结点时,它的所有兄弟已被选择,因此父结点也应该被选择的情况,如有需要,请读者自己完成。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航