private m_valuemember as string private m_displaymember as string <category("data")> _ public property valuemember() as string get return m_valuemember end get set(byval value as string) m_valuemember = value end set end property <category("data")> _ public property displaymember() as string get return m_displaymember end get set(byval value as string) m_displaymember = value end set end property
dim mynewleafnode as treeleafnodedim currobject as objectcurrobject = cm.list(currentlistindex)if me.displaymember <> "" andalso me.valuemember <> "" then ' 添加叶节点? dim pdvalue as system.componentmodel.propertydescriptor dim pddisplay as system.componentmodel.propertydescriptor pdvalue = cm.getitemproperties()(me.valuemember) pddisplay = cm.getitemproperties()(me.displaymember) mynewleafnode = _ new treeleafnode(cstr(pddisplay.getvalue(currobject)), _ currobject, _ pdvalue.getvalue(currobject), _ currentlistindex)
protected overrides sub onafterselect _ (byval e as system.windows.forms.treevieweventargs) dim tln as treeleafnode if typeof e.node is treegroupnode then tln = findfirstleafnode(e.node) dim groupargs as new grouptreevieweventargs(e) raiseevent aftergroupselect(groupargs) elseif typeof e.node is treeleafnode then dim leafargs as new leaftreevieweventargs(e) raiseevent afterleafselect(leafargs) tln = ctype(e.node, treeleafnode) end if if not tln is nothing then if cm.position <> tln.position then cm.position = tln.position end if end if mybase.onafterselect(e)end sub
private function findfirstleafnode(byval currnode as treenode) _ as treeleafnode if typeof currnode is treeleafnode then return ctype(currnode, treeleafnode) else if currnode.nodes.count > 0 then return findfirstleafnode(currnode.nodes(0)) else return nothing end if end ifend function
private sub cm_positionchanged(byval sender as object, _ byval e as system.eventargs) handles cm.positionchanged dim tln as treeleafnode if typeof me.selectednode is treeleafnode then tln = ctype(me.selectednode, treeleafnode) else tln = findfirstleafnode(me.selectednode) end if if tln.position <> cm.position then me.selectednode = findnodebyposition(cm.position) end ifend subprivate overloads function findnodebyposition(byval index as integer) _ as treenode return findnodebyposition(index, me.nodes)end functionprivate overloads function findnodebyposition(byval index as integer, _ byval nodestosearch as treenodecollection) as treenode dim i as integer = 0 dim currnode as treenode dim tln as treeleafnode do while i < nodestosearch.count currnode = nodestosearch(i) i += 1 if typeof currnode is treeleafnode then tln = ctype(currnode, treeleafnode) if tln.position = index then return currnode end if else currnode = findnodebyposition(index, currnode.nodes) if not currnode is nothing then return currnode end if end if loop return nothingend function
private treegroups as new arraylist()public sub removegroup(byval group as group) if not treegroups.contains(group) then treegroups.remove(group) groupingchanged() end ifend subpublic overloads sub addgroup(byval group as group) try treegroups.add(group) groupingchanged() catch end tryend subpublic overloads sub addgroup(byval name as string, _ byval groupby as string, _ byval displaymember as string, _ byval valuemember as string, _ byval imageindex as integer, _ byval selectedimageindex as integer) dim mynewgroup as new group(name, groupby, _ displaymember, valuemember, _ imageindex, selectedimageindex) me.addgroup(mynewgroup)end subpublic function getgroups() as group() return ctype(treegroups.toarray(gettype(group)), group())end function
public event beforegroupselect _ (byval sender as object, byval e as grouptreeviewcanceleventargs)public event aftergroupselect _ (byval sender as object, byval e as grouptreevieweventargs)public event beforeleafselect _ (byval sender as object, byval e as leaftreeviewcanceleventargs)public event afterleafselect _ (byval sender as object, byval e as leaftreevieweventargs)protected overrides sub onbeforeselect _ (byval e as system.windows.forms.treeviewcanceleventargs) if typeof e.node is treegroupnode then dim groupargs as new grouptreeviewcanceleventargs(e) raiseevent beforegroupselect(cobj(me), groupargs) elseif typeof e.node is treeleafnode then dim leafargs as new leaftreeviewcanceleventargs(e) raiseevent beforeleafselect(cobj(me), leafargs) end if mybase.onbeforeselect(e)end subprotected overrides sub onafterselect _ (byval e as system.windows.forms.treevieweventargs) dim tln as treeleafnode if typeof e.node is treegroupnode then tln = findfirstleafnode(e.node) dim groupargs as new grouptreevieweventargs(e) raiseevent aftergroupselect(cobj(me), groupargs) elseif typeof e.node is treeleafnode then dim leafargs as new leaftreevieweventargs(e) raiseevent afterleafselect(cobj(me), leafargs) tln = ctype(e.node, treeleafnode) end if if not tln is nothing then if cm.position <> tln.position then cm.position = tln.position end if end if mybase.onafterselect(e)end sub
注意:本示例应用程序(名为“thesample”)包含在本文的下载中。
新闻热点
疑难解答