TreeView控件 這個控件的使用率也是很高的,所以我們就應(yīng)該好好掌握一下了。在梅子這兒有一個TreeView控件的“玩轉(zhuǎn)系列”。很好!我也就是把她 里面的一些精華截下來,給大家少作說明而已。如果你想系統(tǒng)的學(xué)一下呢,就下一個“玩轉(zhuǎn)系列”吧! 簡單的介紹: TreeView控件用來顯示信息的分級視圖,如同Windows里的資源管理器的目錄。TreeView控件中的各項信息都有一個與之相關(guān)的Node對 象。TreeView顯示Node對象的分層目錄結(jié)構(gòu),每個Node對象均由一個Label對象和其相關(guān)的位圖組成。在建立TreeView控件后,我們可以展開和 折疊、顯示或隱藏其中的節(jié)點。TreeView控件一般用來顯示文件和目錄結(jié)構(gòu)、文檔中的類層次、索引中的層次和其他具有分層目錄結(jié)構(gòu)的信息 。 TreeView控件有3個屬性頁,General頁、Font頁、Picture頁。 TreeView控件屬性 1。LineStyle屬性 該屬性設(shè)置本項控件中Node對象之間顯示的線條類型。格式: 表單.TreeView名.LineStyle=值 值的范圍: 0-TvwTreeLines (缺省)三線顯示兄弟節(jié)點和父子節(jié)點之間的連線 1-TvwRootLines 根(Root)線。除了上述的連線以外,還顯示根目錄之間的連線 需要注意的是:在設(shè)置該屬性前,必須設(shè)置Style屬性為TreeLine(三線)。 2。Style屬性 該屬性返回和設(shè)置表現(xiàn)在TreeView控件對象中Node對象的圖形類型和文本類型。格式: 表單.TreeView名.Style=值 值的范圍: 0-TvwTextOnly 文本 1-TvwPictureText 圖象和文本 2-TvwPlusMinusText +/-以及文本 3-TvwPlusPictureText +/-、圖象以及文本 4-TvwTreeLinesText 連線和文本 5-TvwTreeLinesPictureText 連線、圖象和文本 6-TvwTreeLinesPlusMinueText 連線、+/-和文本 7-TvwTreeLinesPlusMinusPictureText (缺省)連線、+/-、圖象和文本 3。Indentation屬性 該屬性設(shè)置各個新的Node對象縮進的量度。格式如下: 表單.TreeView名.Indentation=值 值的范圍: 必須大于0的數(shù),缺省值為38.00,默認單位是磅 4。Nodes屬性和Index屬性 該屬性設(shè)置訪問TreeView控件使用Nodes對象集合,Index指定Nodes集合中的某一個節(jié)點。訪問格式: 表單.TreeView名.Nodes(index) 例如要表示當前表單中Treenode1控件的Nodes的第三個節(jié)點為: thisform.treenode1.Nodes(3) Nodes集合的屬性 1。Nodes集合的屬性 該屬性指定當前Nodes集合中的節(jié)點個數(shù)。格式: 表單.TreeView名.Nodes.Count 2。Text屬性 該屬性指定Nodes集合中某個節(jié)點的文字信息。格式: 表單.TreeView名.Nodes(index).Text 3。Key屬性 該屬性指定Nodes集合中某個節(jié)點的關(guān)鍵字。格式: 表單.TreeView名.Nodes(index).Key 4。另外還有對Nodes集合中某個節(jié)點進行操作的屬性Root(返回當前節(jié)點的根結(jié)節(jié))、FirsSibling(第一個兄弟節(jié)點)和Next(下一個兄弟節(jié)點)。 例如: thisform.treeview1.Nodes(3).Root.Key thisform.treeview1.nodes(3).firstsibling.key thisform.treeview1.nodes(3).next.key 上面的語句就分別返回了當前表單中treeview1控件的第三個節(jié)點的根結(jié)節(jié)、第一個兄弟節(jié)點和下一個兄弟節(jié)點的關(guān)鍵字。 Nodes集合的方法 1。Clear方法 該方法清除當前Nodes集合中的所有節(jié)點。格式: 表單.TreeView名.Nodes.Clear 2。Add方法 該方法向當前Nodes集合中添加一個新節(jié)點。格式: 表單.TreeView名.Nodes.Add(Relative,RelationShip,Key,Text,Image,Selectedimage) 解釋: Relative 表示相關(guān)聯(lián)的親屬,一般用其親屬的關(guān)鍵字表示 RelationShip 表示與關(guān)聯(lián)的親屬之間的關(guān)系。 1-tvwNext 表示親屬間是平等關(guān)系 4-tvwNext 表示親屬間是父子關(guān)系 Key 表示此節(jié)點的關(guān)鍵字,也是區(qū)別于其他節(jié)點的唯一標識 Text 表示此節(jié)點要顯示的文本 Image 表示節(jié)點處顯示的圖形 舉例: thisform.treeview1.nodes.add(,"r","root") &&增加根節(jié)點r thisform.treeview1.nodes.add("r",4,"c1","child1") &&增加子節(jié)點c1 TreeView控件的常用事件 1。NodeClick事件 該事件在用戶單擊節(jié)點文本時激活。格式: PROCEDURE TreeView名.NodeClick LPARAMETERS node && node表示當前節(jié)點對象 &&你的程序 ENDPROC 2。Expand事件 該事件在用戶展開一個"+"節(jié)點時激活。格式: PROCEDURE TreeView名.Ecpand LPARAMERS node &&你的程序 ENDPROC 3。Collapse事件 該事件在用戶折疊一個"-"節(jié)點時激活。格式: PROCEDURE TreeView名.Collapse LPATAMETERS node &&你的程序 ENDPROC 簡單的例子(有關(guān)用數(shù)據(jù)表作為數(shù)據(jù)環(huán)境給TreeView控件增加節(jié)點,其中再次使用了imagelist控件) 建立一個表單treeview1.scx,其數(shù)據(jù)環(huán)境為zgb.dbf,表結(jié)構(gòu)如下: xm bmh bm ------------------------- 王華 001 人事處 李名 002 財務(wù)處 張應(yīng) 003 科研處 張強 001 人事處 李冰 002 財務(wù)處 孫樣 002 財務(wù)處 理應(yīng) 003 科研出 ------------------------- 向該表單中添加一個treeview控件treeview1和一個圖象列表控件imagelist1,向imagelist1控件中插入兩幅圖片。在imagelist1控件上設(shè)計如 下事件: PROCEDURE imagelist1.click thisform.treeview1.linestyle=1 thisform.treeview1.style=7 select distinct bmh,bm from zgb into curs temp order by bmh selec temp go top do while .not.eof() this.nodes.add(,,'K'+temp.bmh,rtrim(temp.bm)) sele zgb set filter to bmh=temp.bmh go top do while .not.eof() this.nodes.add('K'+TEMP.BMH,4,,RTRIM(XM)) skip enddo sele temp skip enddo this.imagelist=thisform.imagelist1.object for imagelist=1 to this.nodes.count if this.nodes(imagelist).children>0 this.nodes(imagelist).image='bmkey' else this.nodes(imagelist).image='zgkey' endi endfor ENDPROC <完> 下貼還有一個復(fù)雜一些的例子 相對復(fù)雜的例子其中使用了類 建立一個表單treevies2.scx,在其中添加一個treeview控件oletree、7個命令按鈕、一個形狀、一個組合框、4個標簽。 對表單中對應(yīng)的類treeview2的定義如下: ******************************************* **類:treeview2 ******************************************* DEFINE CLASS treeview2 AS form DataSession=2 top=19 left=13 height=297 width=568 docreate=.t. caption="treeview" maxbutton=.f. windowState=0 cnextkey=1_ cdbfname="" name="form1" *如果一個dbf不能打開,則設(shè)置為.f. openedsuccessfully=.f. ADD OBJECT oletree AS olecontrol with; top=79,; left=11,; height=182,; tabindex=1,; name="oletree" ADD OBJECT cboparent AS combobox with; fontbold=.f.,; fontname="宋體",; fontsize=10,; rowsource="",; value=0,; controlsource="",; height=23,; left=236,; style=2,; tabindex=10,; top=267,; width=242,; name="cboParent" ADD OBJECT label1 AS label with; autosize=.t.,; fontbold=.t.,; fontname="宋體",; fontsize=12,; backstyle=0,; caption="選擇節(jié)點",; height=20,; left=159,; top=271,; width=71,; tabindex=9,; forecolor=rgb(0,0,255),; name="label1" ADD OBJECT cmdnewnode AS commandbutton with; top=79,; left=483,; height=26,; width=82,; fontbold=.t.,; fontname="宋體",; fontsize=11,; caption="新根節(jié)點",; enabled=.t.,; tabindex=2,; forecolor=rgb(255,0,0),; name="cmdNewNode" ADD OBJECT cmddeletenode AS commandbutton with; top=136,; left=483,; height=26,; width=82,; fontbold=.t.,; fontname="宋體",; fontsize=11,; caption="刪 除",; enabled=.t.,; tabindex=4,; forecolor=rgb(255,0,0),; name="cmdDeleteNode" ADD OBJECT cmdloaddbf AS commandbutton with; top=225,; left=483,; height=26,; width=82,; fontbold=.t.,; fontname="宋體",; fontsize=11,; caption="從DBF調(diào)用",; enabled=.t.,; tabindex=7,; forecolor=rgb(255,0,0),; name="cmdloaddbf" ADD OBJECT cmdsavedbf AS commandbutton with; top=195,; left=483,; height=26,; width=82,; fontbold=.t.,; fontname="宋體",; fontsize=11,; caption="存儲到DBF",; enabled=.t.,; tabindex=6,; forecolor=rgb(255,0,0),; name="cmdsavedbf" ADD OBJECT cmdclear AS commandbutton with; top=165,; left=483,; height=26,; width=82,; fontbold=.t.,; fontname="宋體",; fontsize=11,; caption="清 除",; enabled=.t.,; tabindex=5,; forecolor=rgb(255,0,0),; name="cmdclear" ADD OBJECT cmdnewchild AS commandbutton with; top=107,; left=483,; height=26,; width=82,; fontbold=.t.,; fontname="宋體",; fontsize=11,; caption="新子節(jié)點",; enabled=.t.,; tabindex=3,; forecolor=rgb(255,0,0),; name="cmdnewchild" ADD OBJECT cmdclose AS commandbutton with; top=267,; left=485,; height=26,; width=80,; fontbold=.t.,; fontname="宋體",; fontsize=11,; caption="關(guān) 閉",; enabled=.t.,; tabindex=5,; forecolor=rgb(255,0,0),; name="cmdclose" ADD OBJECT shape1 AS shape with; top=13,; left=11,; height=56,; width=548,; backstyle=0,; specialeffect=0,; name="shape1" ADD OBJECT label3 AS label with; top=24,; left=21,; height=40,; width=529,; fontbold=.t.,; fontname="宋體",; fontsize=10,; wordwrap=.t.,; tabindex=0,; forecolor=rgb(0,0,255),; name="label3",; caption="Treeview控件顯示一個類似瀏覽器的界面。單擊“新根節(jié)點”添加一個新項到該控件中。單擊“新子節(jié)點”添加一個; 子節(jié)點到當前選擇的節(jié)點上??梢詥螕?#8220;存儲到DBF”將這些節(jié)點存儲到一個DBF文件中。" ADD OBJECT label4 AS label with; autosize=.t.,; top=7,; left=19,; height=19,; width=85,; fontbold=.t.,; fontname="宋體",; fontsize=11,; wordwrap=.t.,; tabindex=0,; forecolor=rgb(255,0,0),; name="label4",; caption="操作說明" *!*返回新節(jié)點的一個新關(guān)鍵字 PROCEDDURE newkey cKey=this.cNextKey this.cNextKey=allt(str(val(this.cNextKey)+1)+"_") PRTURN cKey ENDPROC PROCEDURE refreshcombo this.cboparent.clear for i=1 to this.oletree.nodes.count this.cboparent.addlistitem(this.oletree.nodes.item(i)).fullpath,i,1) endfor ENDPROC *!*檢查確保該表具有存儲或恢復(fù)一個輪廓所需要的字段 PROCEDURE verifytablestructure LOCAL lafields[1,11],lnpos,laneeded[3],lncnt #DEFINE FIELD1_LOC"KEY" #DEFINE FIELD2_LOC"PARENT" #DEFINE FIELD3_LOC"TEXT" laNeeded[1]=FIELD1_LOC laNeeded[2]=FIELD2_LOC laNeeded[3]=FIELD3_LOC =AFIELDS(laFields,laNeeded[lnCnt]) FOR lnCnt=1 to alen(laNeeded) lnPos=ASCAN(laFields,laNeeded[lnCnt]) IF lnPos=0 .or. laFields[lnPos+1]!='C' #DEFINE TITLE_LOC "Invalid Table Structure" #DEFINE MSG_LOC"The table must contain 3 character fields:"; +chr(13)+chr(13)+FIELD1_LOC+; chr(13)+FIELD2_LOC+chr(13)+FIELD3_LOC =messagebox(msg_loc,48+0+0,title_loc) return .f. ENDIF ENDFOR return .t. ENDPROC PROCEDURE opendbf LPARAMETERS lcDBFNAME,llExclusive thisform.openedsuccessfully=.t. #DEFINE ERR_LOC "Error" IF !file (lcDBFNAME) #DEFINE ERR1_LOC "Cannot find the specified file." =messagebox(err1_loc,48+0+0,err_loc) return .f. ENDIF lcAlias=substr(lcDBFName,rat("\",lcDBFNAME)+1) lcAlias=substr(lcAlias,1,at(".",lcAlias)-1) IF llExclusive use (lcDBFName) in 0 exclusive ENDIF IF !use (lcAlias) use (lcDBFName) in 0 shared ENDIF select (lcAlias) return thisform.openedsuccessfully ENDPROC *!*基于選擇的項來允許/廢止相應(yīng)的按鈕 PROCEDURE checkbuttons o=thisform.oletree IF isnull(o.selecteditem)then thisform.cmdnewchild.enabled=.f. thisform.cmdndeletenode.enabled=.f. ELSE thisform.cmdnewchild.enabled=.T. thisform.cmdndeletenode.enabled=.T. ENDIF ENDPROC PROCEDURE init *!*用init 來檢查OCX是否安裝和裝入 IF type("this.oletree")#"o" .or. isnull(this.oletree) return .f. ENDIF ENDPROC PROCEDURE oletree.afterlabeledit LPARAMETERS cancel,newstring IF !isnull(newstring) this.selecteditem.text=newstring thisform.refreshcombo thisform.cboparent.value=this.selecteditem.index EDNIF ENDPROC PROCEDURE oletree.nodeclick LPARAMETERS node thisform.refreshcombo thisform.cboparent.value=node.index thisofrm.checkbuttons ENDPROC PROCEDURE oletree.lostfocus on key label F1 ENDPROC PROCEDURE oletree.gotfocus on key label F1 help id_screen.activeform.helpcontextID ENDPROC PROCEDURE cboparent.gotfocus thisform.refreshcombo thisform.checkbuttons ENDPROC PROCEDURE cboparent.interactivechange thisform.oletree.nodes(this.value).selected=.t. thisform.checkbuttons ENDPROC PROCEDURE cmdnewnode.click #DEFINE TXT_LOC"單擊編輯文本" o=thisform.oletree o.nodes.add(,1,thisform.newkey(),TXT_LOC,0) ENDPROC PROCEDURE cmddeletenode.click o=thisform.oletree IF !isnull(o.selecteditem) o.nodes.remove(o.selceteditem.key) ENDIF thisform.checkbuttons ENDPROC PROCEDURE cmdloaddbf.click local lcOldAlias,laFields lcOldAlias=alias() lcDBFName=getfile('dbf') IF empty(m.lcDBFName) return ENDIF IF thisform.opendbf(lcdbfname) IF !thisform.verifytablestructure() return ENDIF o=thisform.oletree.nodes o.clear SCAN IF allt(parent)='0_' o.add(,1,allt(key),allt(text),0) ELSE o.add(allt(parent),4,allt(key,allt(text),0) ENDIF thisform.cNextKey=allt(str(val(key)+1)+"_") ENDSCAN use IF !empty(lcOldAlias) select (lcOldAlias) ENDIF ENDIF thisform.checkbuttons ENDPROC PROCEDURE cmdsavedbf.click local loNodes,lcParent,lcDBFName,lcOldAlias,lcOldSafety #DEFINE WARNING_LOC "Continuing will destroy all data in the table; and create a new table with three fields."+chr(13)+"Do you want to continue?" #DEFINE WARN_LOC "WARNING" lcOldAlias=alias() lcOldSafety=set("safety") lcDBFName=getfile('dbf') IF empty(lcDBFName) && 用戶選擇放棄 return ENDIF IF file(lcDBFName) IF thisform.opendbf(lcDBFName,.t.) .and. ; thisform.verifytablestructure() .and.; messagebox(warning_loc,48+256+4,warn_loc)=6 set safety off zap set safety &lcOldSafety ELSE return ENDIF ELSE create table (lcDBFName) (key c(4), parent c(4), text c(60)) ENDIF loNodes=thisform.oletree.nodes FOR i=1 to loNodes.count IF isnull(loNodes.Item(i).parent) lcparent="0_" &&根 ELSE lcParent=loNodes.item(i)parent.key ENDIF insert into (lcDBFName) values (loNodes.item(i).key, lcParent, loNodes.item(i).text) ENDFOR use IF !empty(lcOldAlias) select (lcOldAlias) ENDIF thisform.checkbuttons ENDPROC PROCEDURE cmdclear.click thisform.oletree.nodes.clear thisform.checkbuttons ENDPROC PROCEDURE cmdnewchild.click #DEFINE TXT_LOC "單擊編輯文本" o= thisform.oletree IF !isnull(o.selecteditem) then o.Nodes.add(o.seleceteditem.key,4,thisform.newkey(),txt_loc,0) ENDIF ENDPROC PROCEDURE cmdclose.click thisform.release ENDPROC ENDDDEFINE |
|