| 发表于:2008-01-22 19:01:4412楼 得分:0 |
以下为添加目录树的源码,希望对你有帮助: '添加下一级 private sub cmdchild_click() dim onodex as node dim skey as string dim iindex as integer lb = "" iindex = treeview1.selecteditem.index 'check to see if a node is selected skey = getnextkey() ' get a key for the new node frmlb.show vbmodal if lb & "" = "" then exit sub else set onodex = treeview1.nodes.add(iindex, tvwchild, skey, lb, 1, 2) cmdsave.enabled = true exit sub end if end sub '添加同一级 private sub cmdfirst_click() dim skey as string dim iindex as integer lb = "" if addnode = "物品分类" then msgbox "对不起!不能再次添加根目录!" exit sub else iindex = treeview1.selecteditem.index 'check to see if a node is selected skey = getnextkey() ' get a key for the new node end if frmlb.show vbmodal if lb & "" = "" then else treeview1.nodes.add iindex, tvwlast, skey, lb, 1, 2 cmdsave.enabled = true exit sub end if end sub private sub getfirstparent() 'find the first parent node in the treeview on error goto myerr dim i as integer dim ntmp as integer for i = 1 to treeview1.nodes.count 'this will give an error if there is no parent ntmp = treeview1.nodes(i).parent.index next exit sub myerr: mnindex = i exit sub end sub private function getnextkey() as string 'returns a new key value for each node being added to the treeview 'this algorithm is very simple and will limit you to adding a total of 999 nodes 'each node needs a unique key. if you allow users to remove nodes you can't use 'the nodes count +1 as the key for a new node. dim snewkey as string dim ihold as integer dim i as integer on error goto myerr 'the next line will return error #35600 if there are no nodes in the treeview ihold = val(treeview1.nodes(1).key) for i = 1 to treeview1.nodes.count if val(treeview1.nodes(i).key) > ihold then ihold = val(treeview1.nodes(i).key) end if next ihold = ihold + 1 snewkey = cstr(ihold) & "_" getnextkey = snewkey 'return a unique key exit function myerr: 'because the treeview is empty return a 1 for the key of the first node getnextkey = "1_" exit function end function private sub showtree() 'asks the user to find the database and then restores the nodes 'in the treeview control from the table dim onodex as node dim nimage as integer dim nselectedimage as integer dim stablenames as string 'set mdb = dbengine.workspaces(0).opendatabase(app.path & "\cx.mdb", false, false, ";pwd=nbmidt920") treeview1.nodes.clear 'clear the treeview of any nodes 'set mrs = mdb.openrecordset("splb") rs.open "select * from splb", cn, adopenstatic, adlockoptimistic if rs.recordcount > 0 then 'make sure there are records in the table rs.movefirst do while rs.eof = false nimage = rs.fields("image") nselectedimage = rs.fields("selectedimage") if trim(rs.fields("parent")) = "0_" then 'all root nodes have 0_ in the parent field set onodex = treeview1.nodes.add(, 1, trim(rs.fields("key")), _ trim(rs.fields("text")), nimage, nselectedimage) else 'all child nodes will have the parent key stored in the parent field set onodex = treeview1.nodes.add(trim(rs.fields("parent")), tvwchild, _ trim(rs.fields("key")), trim(rs.fields("text")), nimage, nselectedimage) 'expend the treeview so all nodes are visible end if rs.movenext loop end if treeview1.nodes.item(1).expanded = true rs.close 'close the table 'mdb.close 'close the database end sub sub savetotable() dim sresponse as string dim smdbname as string dim stablename as string dim i as integer set mrs = new adodb.recordset mrs.open "select * from splb", cn, adopenstatic, adlockoptimistic writetotable 'go to the sub that writes the nodes into the table mrs.close 'close the recordset end sub private sub cmdok_click() if addnode = "物品分类" or addnode = "" then msgbox "请选择物品分类信息!" else lb = addnode unload me end if end sub '删除所选类别 private sub cmdremove_click() dim iindex as integer if addnode = "物品分类" then msgbox "无法删除当前选定分类!" else if msgbox("确定要删除当前选定分类吗?", 36) = 6 then iindex = treeview1.selecteditem.index 'check to see if a node is selected treeview1.nodes.remove iindex 'removes the node and any children it has savetotable end if end if end sub '保存 private sub cmdsave_click() call savetotable end sub sub writetotable() dim itmp as integer dim iindex as integer if mrs.recordcount > 0 then mrs.movefirst do while mrs.eof = false mrs.delete mrs.movenext loop end if getfirstparent 'find a root node in the treeview 'get the index of the root node that is at the top of the treeview iindex = treeview1.nodes(mnindex).firstsibling.index itmp = iindex mrs.addnew mrs("parent") = "0_" 'this is a root node mrs("key") = treeview1.nodes(iindex).key mrs("text") = treeview1.nodes(iindex).text mrs("image") = treeview1.nodes(iindex).image mrs("selectedimage") = treeview1.nodes(iindex).selectedimage mrs.update 'if the node has children call the sub that writes the children if treeview1.nodes(iindex).children > 0 then writechild iindex end if while iindex <> treeview1.nodes(itmp).lastsibling.index 'loop through all the root nodes mrs.addnew mrs("parent") = "0_" 'this is a root node mrs("key") = treeview1.nodes(iindex).next.key mrs("text") = treeview1.nodes(iindex).next.text mrs("image") = treeview1.nodes(iindex).next.image mrs("selectedimage") = treeview1.nodes(iindex).next.selectedimage mrs.update 'if the node has children call the sub that writes the children if treeview1.nodes(iindex).next.children > 0 then writechild treeview1.nodes(iindex).next.index end if ' move to the next root node iindex = treeview1.nodes(iindex).next.index wend cmdsave.enabled = false end sub '------------------------------------- private sub writechild(byval inodeindex as integer) dim itempindex as integer itempindex = treeview1.nodes(inodeindex).child.firstsibling.index for i = 1 to treeview1.nodes(inodeindex).children mrs.addnew---- mrs("parent") = treeview1.nodes(itempindex).parent.key mrs("key") = treeview1.nodes(itempindex).key mrs("text") = treeview1.nodes(itempindex).text mrs("image") = treeview1.nodes(itempindex).image mrs("selectedimage") = treeview1.nodes(itempindex).selectedimage mrs.update if treeview1.nodes(itempindex).children > 0 then writechild (itempindex) end if if i <> treeview1.nodes(inodeindex).children then itempindex = treeview1.nodes(itempindex).next.index end if next i end sub | | |
|