PB樹形控件勾選聯動

做PB樹形控件的時候需要勾選聯動,從網上找了個方案,但是對向上勾選支持的不是很好,自己做了點優化,希望可以幫到別人

首先treeview需要勾選 checkboxes屬性

在tv_1.clicked事件中增加代碼

TreeViewItem ltvitem
getitem(handle, ltvitem)

post event ue_statechanged(handle, ltvitem.statepictureindex)

在樹形控件中新增函數 ue_statechanged(long handle,integer prevstate)   代碼如下

treeviewitem ltvi_item
getitem(handle, ltvi_item)

if ltvi_item.statepictureindex = prevstate then
    return
else
    this.Event ue_synchronizechildren(handle,ltvi_item.statepictureindex)    //這個函數包含了向下的選中和取消
    this.Event ue_synchronizeparent(handle,ltvi_item.statepictureindex)       //優化了這個函數
end if

在樹形控件新增函數 ue_synchronizechildren(long handle,integer state) 代碼如下

long ll_childitem
treeviewitem ltvi_item
getitem(handle, ltvi_item)
ltvi_item.statepictureindex = state
setitem(handle, ltvi_item)
ll_childitem = this.finditem(ChildTreeItem!, handle)
do while(ll_childitem <> -1)
    this.Event ue_synchronizechildren(ll_childitem, state) //遞歸遍歷後代結點
    ll_childitem = this.finditem(NextTreeItem!, ll_childitem)
loop

在樹形控件新增函數 ue_synchronizeparent(long handle,integer state) 代碼如下

long ll_parentitem,ll_nextitem
treeviewitem ltvi_item,ltvi_nextitem
this.getitem(handle, ltvi_item)
ltvi_item.statepictureindex = state
this.setitem(handle, ltvi_item)
if state = 1 then
    ll_parentitem = this.finditem(ParentTreeItem!, handle)
    if ll_parentitem > 0 then
        this.getitem(ll_parentitem, ltvi_item)
        ltvi_item.statepictureindex = state
        this.Event ue_synchronizeparent(ll_parentitem, state)
    else
        return
    end if
else
    ll_nextitem = this.finditem(NextTreeItem!, handle) //同層下一項
    do while(ll_nextitem <> -1)
        getitem(ll_nextitem, ltvi_nextitem)
        if ltvi_nextitem.statepictureindex = 1 then return //有沒選中,就返回!
        ll_nextitem = this.finditem(NextTreeItem!, ll_nextitem)
    loop
    ll_nextitem = this.finditem(previoustreeItem!, handle) //同層上一項
    do while(ll_nextitem <> -1)
        getitem(ll_nextitem, ltvi_nextitem)
        if ltvi_nextitem.statepictureindex = 1 then return //有沒選中,就返回!
        ll_nextitem = this.finditem(previoustreeItem!,ll_nextitem)
    loop
    ll_parentitem = this.finditem(ParentTreeItem!, handle)
    if ll_parentitem > 0 then
        this.getitem(ll_parentitem, ltvi_item)
        ltvi_item.statepictureindex = state
        this.Event ue_synchronizeparent(ll_parentitem, state)
    else
        return
    end if
end if
this.Event ue_synchronizeparent(ll_parentitem,state)


這樣這個樹形控件可以更完美的實現勾選功能,代碼基於前輩代碼做的優化,具體出處現在找不到了,感謝






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章