實現:treeview的同步選擇(checkboxes)的用法
1、當選擇了父節點時,所有的子節點都已選擇
2、當一個子節點未選擇時,父節點可以選擇
本人總結了各方神人給出的checkboxes用法代碼,不是為了居功,只是給各位多提供一條搜尋路徑。參考來自CSDN
代碼:
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
2、為 treeview 添加使用者事件 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
3、為 treeview 添加一個使用者事件 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
long nextitem
treeviewitem ntvitem
nextitem=this.finditem(nexttreeItem!, handle) //同層下一項
do while(nextitem<>-1)
getitem(handle, ntvitem)
if ntvitem.statepictureindex=1 then return //有沒選中,就返回!
nextitem=this.finditem(NextTreeItem!, nextitem)
loop
nextitem=this.finditem(previoustreeItem!, handle) //同層上一項
do while(nextitem<>-1)
getitem(handle, ntvitem)
if ntvitem.statepictureindex=1 then return //有沒選中,就返回!
nextitem=this.finditem(previoustreeItem!,nextitem)
loop
this.Event ue_synchronizeparent(handle, tvitem.statepictureindex)
end if
end if
4、在 treeview 的clicked 事件裡寫入
treeviewitem tvitem
getitem(handle, tvitem)
post event ue_statechanged(handle, tvitem.statepictureindex)
完成上面四步即可完整滿足使用者要求,只需將代碼粘入即可,下面為貼圖