TreeView控件 这个控件的使用率也是很高的,所以我们就应该好好掌握一下了。在梅子这儿有一个TreeView控件的“玩转系列”。很好!我也就是把她 里面的一些精华截下来,给大家少作说明而已。如果你想系统的学一下呢,就下一个“玩转系列”吧! 简单的介绍: TreeView控件用来显示信息的分级视图,如同Windows里的资源管理器的目录。TreeView控件中的各项信息都有一个与之相关的Node对 象。TreeView显示Node对象的分层目录结构,每个Node对象均由一个Label对象和其相关的位图组成。在建立TreeView控件后,我们可以展开和 折叠、显示或隐藏其中的节点。TreeView控件一般用来显示文件和目录结构、文档中的类层次、索引中的层次和其他具有分层目录结构的信息 。 TreeView控件有3个属性页,General页、Font页、Picture页。 TreeView控件属性 1。LineStyle属性 该属性设置本项控件中Node对象之间显示的线条类型。格式: 表单.TreeView名.LineStyle=值 值的范围: 0-TvwTreeLines (缺省)三线显示兄弟节点和父子节点之间的连线 1-TvwRootLines 根(Root)线。除了上述的连线以外,还显示根目录之间的连线 需要注意的是:在设置该属性前,必须设置Style属性为TreeLine(三线)。 2。Style属性 该属性返回和设置表现在TreeView控件对象中Node对象的图形类型和文本类型。格式: 表单.TreeView名.Style=值 值的范围: 0-TvwTextOnly 文本 1-TvwPictureText 图象和文本 2-TvwPlusMinusText +/-以及文本 3-TvwPlusPictureText +/-、图象以及文本 4-TvwTreeLinesText 连线和文本 5-TvwTreeLinesPictureText 连线、图象和文本 6-TvwTreeLinesPlusMinueText 连线、+/-和文本 7-TvwTreeLinesPlusMinusPictureText (缺省)连线、+/-、图象和文本 3。Indentation属性 该属性设置各个新的Node对象缩进的量度。格式如下: 表单.TreeView名.Indentation=值 值的范围: 必须大于0的数,缺省值为38.00,默认单位是磅 4。Nodes属性和Index属性 该属性设置访问TreeView控件使用Nodes对象集合,Index指定Nodes集合中的某一个节点。访问格式: 表单.TreeView名.Nodes(index) 例如要表示当前表单中Treenode1控件的Nodes的第三个节点为: thisform.treenode1.Nodes(3) Nodes集合的属性 1。Nodes集合的属性 该属性指定当前Nodes集合中的节点个数。格式: 表单.TreeView名.Nodes.Count 2。Text属性 该属性指定Nodes集合中某个节点的文字信息。格式: 表单.TreeView名.Nodes(index).Text 3。Key属性 该属性指定Nodes集合中某个节点的关键字。格式: 表单.TreeView名.Nodes(index).Key 4。另外还有对Nodes集合中某个节点进行操作的属性Root(返回当前节点的根结节)、FirsSibling(第一个兄弟节点)和Next(下一个兄弟节点)。 例如: thisform.treeview1.Nodes(3).Root.Key thisform.treeview1.nodes(3).firstsibling.key thisform.treeview1.nodes(3).next.key 上面的语句就分别返回了当前表单中treeview1控件的第三个节点的根结节、第一个兄弟节点和下一个兄弟节点的关键字。 Nodes集合的方法 1。Clear方法 该方法清除当前Nodes集合中的所有节点。格式: 表单.TreeView名.Nodes.Clear 2。Add方法 该方法向当前Nodes集合中添加一个新节点。格式: 表单.TreeView名.Nodes.Add(Relative,RelationShip,Key,Text,Image,Selectedimage) 解释: Relative 表示相关联的亲属,一般用其亲属的关键字表示 RelationShip 表示与关联的亲属之间的关系。 1-tvwNext 表示亲属间是平等关系 4-tvwNext 表示亲属间是父子关系 Key 表示此节点的关键字,也是区别于其他节点的唯一标识 Text 表示此节点要显示的文本 Image 表示节点处显示的图形 举例: thisform.treeview1.nodes.add(,"r","root") &&增加根节点r thisform.treeview1.nodes.add("r",4,"c1","child1") &&增加子节点c1 TreeView控件的常用事件 1。NodeClick事件 该事件在用户单击节点文本时激活。格式: PROCEDURE TreeView名.NodeClick LPARAMETERS node && node表示当前节点对象 &&你的程序 ENDPROC 2。Expand事件 该事件在用户展开一个"+"节点时激活。格式: PROCEDURE TreeView名.Ecpand LPARAMERS node &&你的程序 ENDPROC 3。Collapse事件 该事件在用户折叠一个"-"节点时激活。格式: PROCEDURE TreeView名.Collapse LPATAMETERS node &&你的程序 ENDPROC 简单的例子(有关用数据表作为数据环境给TreeView控件增加节点,其中再次使用了imagelist控件) 建立一个表单treeview1.scx,其数据环境为zgb.dbf,表结构如下: xm bmh bm ------------------------- 王华 001 人事处 李名 002 财务处 张应 003 科研处 张强 001 人事处 李冰 002 财务处 孙样 002 财务处 理应 003 科研出 ------------------------- 向该表单中添加一个treeview控件treeview1和一个图象列表控件imagelist1,向imagelist1控件中插入两幅图片。在imagelist1控件上设计如 下事件: 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 <完> 下贴还有一个复杂一些的例子 相对复杂的例子其中使用了类 建立一个表单treevies2.scx,在其中添加一个treeview控件oletree、7个命令按钮、一个形状、一个组合框、4个标签。 对表单中对应的类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不能打开,则设置为.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="选择节点",; 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="新根节点",; 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调用",; 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="新子节点",; 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="关 闭",; 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控件显示一个类似浏览器的界面。单击“新根节点”添加一个新项到该控件中。单击“新子节点”添加一个; 子节点到当前选择的节点上。可以单击“存储到DBF”将这些节点存储到一个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="操作说明" *!*返回新节点的一个新关键字 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 *!*检查确保该表具有存储或恢复一个轮廓所需要的字段 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 *!*基于选择的项来允许/废止相应的按钮 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 |
|