C#学习笔记TreeView ClassWord文件下载.docx
《C#学习笔记TreeView ClassWord文件下载.docx》由会员分享,可在线阅读,更多相关《C#学习笔记TreeView ClassWord文件下载.docx(14页珍藏版)》请在冰点文库上搜索。
![C#学习笔记TreeView ClassWord文件下载.docx](https://file1.bingdoc.com/fileroot1/2023-5/11/946b8bea-198c-4a1e-865a-81e01e76803f/946b8bea-198c-4a1e-865a-81e01e76803f1.gif)
Syntax:
publicclassTreeView:
Control
Remarks:
TheNodescollectionholdsalltheTreeNodeobjectsthatareassignedtotheTreeViewcontrol.Thetreenodesinthiscollectionarereferredtoastheroottreenodes.Anytreenodethatissubsequentlyaddedtoaroottreenodeisreferredtoasachildnode.BecauseeachTreeNodecancontainacollectionofotherTreeNodeobjects,youmightfinditdifficulttodetermineyourlocationinthetreestructurewhenyouiteratethroughthecollection.YoucanparsetheTreeNode.FullPathstringbyusingthePathSeparatorstringvaluetodeterminewhereaTreeNodelabelbeginsandends.
YoucandisplayimagesnexttothetreenodesbyassigninganImageListtotheImageListpropertyandreferencingtheindexvalueofanImageintheImageListtoassignthatImage.Usethefollowingpropertiestoassignimages:
·
SettheImageIndexpropertytotheindexvalueoftheImagethatyouwanttodisplaywhenatreenodeisnotselected.
SettheSelectedImageIndexpropertytotheindexvalueoftheImagethatyouwanttodisplaywhenatreenodeisselected.
TheimagesreferencedbytheImageIndexandSelectedImageIndexpropertyvaluesarethedefaultimagesdisplayedbyallthetreenodesthatareassignedtotheNodescollection.IndividualtreenodescanoverridethedefaultimagesbysettingtheTreeNode.ImageIndexandTreeNode.SelectedImageIndexproperties.
Treenodescanbeexpandedtodisplaythenextlevelofchildtreenodes.TheusercanexpandtheTreeNodebyclickingtheplus-sign(+)button,ifoneisdisplayednexttotheTreeNode,oryoucanexpandtheTreeNodebycallingtheTreeNode.Expandmethod.ToexpandallthechildtreenodelevelsintheNodescollection,calltheExpandAllmethod.YoucancollapsethechildTreeNodelevelbycallingtheTreeNode.Collapsemethod,ortheusercanpresstheminus-sign(-)button,ifoneisdisplayednexttotheTreeNode.YoucanalsocalltheTreeNode.Togglemethodtoalternatebetweentheexpandedandcollapsedstates.
Treenodescanoptionallydisplaycheckboxes.Todisplaythecheckboxes,settheCheckBoxespropertyoftheTreeViewtotrue.TheCheckedpropertyissettotruefortreenodesthatareinacheckedstate.
Note:
SettingtheTreeNode.CheckedpropertyfromwithintheBeforeCheckorAfterCheckeventcausestheeventtoberaisedmultipletimesandcanresultinunexpectedbehavior.Forexample,youmightsettheCheckedpropertyintheeventhandlerwhenyouarerecursivelyupdatingthechildnodessothattheuserdoesnothavetoexpandandcheckeachoneindividually.Topreventtheeventfrombeingraisedmultipletimes,addlogictoyoureventhandlerthatonlyexecutesyourrecursivecodeiftheActionpropertyoftheTreeViewEventArgsisnotsettoTreeViewAction.Unknown.Foranexampleofhowtodothis,seetheExamplesectionoftheAfterCheckorBeforeCheckevents.
YoucanchangetheappearanceoftheTreeViewcontrolbysettingsomeofitsdisplayandstyleproperties.SettingShowPlusMinustotruedisplaysaplus-signorminus-signbuttonnexttoeachTreeNodethatcanbeexpandedorcollapsed,respectively.SettingtheShowRootLinespropertytotruecausestheTreeViewtodisplaylinesthatjoinalltheroottreenodestogether.YoucandisplaylinesthatconnectchildtreenodestotheirrootnodebysettingtheShowLinespropertytotrue.SettingtheHotTrackingpropertytotruechangestheappearanceofthetreenodelabelsasthemousepointerpassesoverthem.Whenhot-tracked,thetreenodelabelstakeontheappearanceofahyperlink.YoucanalsocompletelycustomizetheappearanceoftheTreeViewcontrol.Todothis,settheDrawModepropertytoavalueotherthanTreeViewDrawMode.NormalandhandletheDrawNodeevent.
WhensettingtheCheckBoxes,Scrollable,ImageIndex,andSelectedImageIndexpropertiesatruntime,theTreeViewhandleisrecreated(seeControl.RecreateHandle)toupdatethecontrol'
sappearance.Thiscausesalltreenodestobecollapsed,exceptfortheselectedTreeNode.
Constructor:
publicTreeView():
InitializesanewinstanceoftheTreeViewclass.
Properties:
1、AllowDrop:
publicvirtualbool型,Getsorsetsavalueindicatingwhetherthecontrolcanacceptdatathattheuserdragsontoit.Thedefaultisfalse.
NotestoInheritors:
WhenoverridingtheAllowDroppropertyinaderivedclass,usethebaseclass'
sAllowDroppropertytoextendthebaseimplementation.Otherwise,youmustprovidealltheimplementation.YouarenotrequiredtooverrideboththegetandsetaccessorsoftheAllowDropproperty;
youcanoverrideonlyoneifneeded.
2、Anchor:
publicvirtualAnchorStyles型,Getsorsetstheedgesofthecontainertowhichacontrolisboundanddetermineshowacontrolisresizedwithitsparent.ThedefaultisTopandLeft.
UsetheAnchorpropertytodefinehowacontrolisautomaticallyresizedasitsparentcontrolisresized.Anchoringacontroltoitsparentcontrolensuresthattheanchorededgesremaininthesamepositionrelativetotheedgesoftheparentcontrolwhentheparentcontrolisresized.
Youcananchoracontroltooneormoreedgesofitscontainer.Forexample,ifyouhaveaFormwithaButtonwhoseAnchorpropertyvalueissettoTopandBottom,theButtonisstretchedtomaintaintheanchoreddistancetothetopandbottomedgesoftheFormastheHeightoftheFormisincreased.
TheAnchorandDockpropertiesaremutuallyexclusive.Onlyonecanbesetatatime,andthelastonesettakesprecedence.
WhenoverridingtheAnchorpropertyinaderivedclass,usethebaseclass'
sAnchorpropertytoextendthebaseimplementation.Otherwise,youmustprovidealltheimplementation.YouarenotrequiredtooverrideboththegetandsetaccessorsoftheAnchorproperty;
youcanoverrideonlyoneifneeded.
3、AutoScrollOffset:
publicvirtualPoint型,GetsorsetswherethiscontrolisscrolledtoinScrollControlIntoView.Thedefaultistheupper-leftcornerofthecontrol.
4、AutoSize:
publicvirtualbool型,ThisAPIsupportsthe.NETFrameworkinfrastructureandisnotintendedtobeuseddirectlyfromyourcode.Thispropertyisnotrelevantforthisclass.
5、BackColor:
publicoverrideColor型,Getsorsetsthebackgroundcolorforthecontrol.ThedefaultisthevalueoftheDefaultBackColorproperty.
6、Bottom:
publicint型,Getsthedistance,inpixels,betweenthebottomedgeofthecontrolandthetopedgeofitscontainer'
sclientarea.
ThevalueofthispropertyisequaltothesumoftheToppropertyvalue,andtheHeightpropertyvalue.
TheBottompropertyisaread-onlyproperty.YoucanmanipulatethispropertyvaluebychangingthevalueoftheToporHeightpropertiesorcallingtheSetBounds,SetBoundsCore,UpdateBounds,orSetClientSizeCoremethods.
7、BorderStyle:
public
BorderStyle型,Getsorsetstheborderstyleofthetreeviewcontrol.
Whenthe
BorderStyle
propertyissetto
Fixed3D,the
TreeView
hasasunken,three-dimensionalappearance.Todisplayaflat,thinborderaroundthe
control,setthe
propertyto
FixedSingle.
8、CanFocus:
publicbool型,Getsavalueindicatingwhetherthecontrolcanreceivefocus.
Inorderforacontroltoreceiveinputfocus,thecontrolmusthaveahandleassignedtoit,andtheVisibleandEnabledpropertiesmustbothbesettotrueforboththecontrolandallitsparentcontrols,andthecontrolmustbeaformorthecontrol'
soutermostparentmustbeaform.
9、CanRaiseEvents:
protectedoverridebool型,Determinesifeventscanberaisedonthecontrol.
IfthiscontrolisbeinghostedasanActiveXcontrol,thispropertywillreturnfalseiftheActiveXcontrolhasitseventsfrozen.
10、CanSelect:
publicbool型,Getsavalueindicatingwhetherthecontrolcanbeselected.
ThispropertyreturnstrueiftheSelectablevalueofSystem.Windows.Forms.ControlStylesissettotrue,iscontainedinanothercontrol,thecontrolitselfisvisibleandenabled,andallitsparentcontrolsarevisibleandenabled.
TheWindowsFormscontrolsinthefollowinglistarenotselectableandwillreturnavalueoffalsefortheCanSelectproperty.Controlsderivedfromthesecontrolsarealsonotselectable.
Panel·
GroupBox·
PictureBox·
ProgressBar·
Splitter·
Label·
LinkLabel(whenthereisnolinkpresentinthecontrol)
11、Capture:
publicbool型,Getsorsetsavalueindicatingwhetherthecontrolhascapturedthemouse.
Whenacontrolhascapturedthemouse,itreceivesmouseinputwhetherornotthecursoriswithinitsborders.Themouseistypicallyonlycapturedduringdragoperations.
Onlytheforegroundwindowcancapturethemouse.Whenabackgroundwindowattemptstodoso,thewindowreceivesmessagesonlyformouseeventsthatoccurwhenthemousecursoriswithinthevisibleportionofthewindow.Also,eveniftheforegroundwindowhascapturedthemouse,theusercanstillclickanotherwindow,bringingittotheforeground.
Whenthemouseiscaptured,shortcutkeysshouldnotwork.
12、CheckBoxes:
publicbool型,Getsorsetsavalueindicatingwhethercheckboxesaredisplayednexttothetreenodesinthetreeviewcontrol.Thedefaultisfalse.
AcheckboxisdisplayedtotheleftofboththetreenodelabelandtreenodeImage,ifany.Checkboxesallowtheusertoselectmorethanonetreenodeatatime.
WhentheCheckBoxespropertyofaTreeViewissettotrue,andtheStateImageListpropertyisset,eachTreeNodethatiscontainedintheTreeViewdisplaysthefirstandsecondimagesfrom