C#学习笔记Form Class.docx
《C#学习笔记Form Class.docx》由会员分享,可在线阅读,更多相关《C#学习笔记Form Class.docx(40页珍藏版)》请在冰豆网上搜索。
![C#学习笔记Form Class.docx](https://file1.bdocx.com/fileroot1/2023-7/24/3845e114-9210-4306-bde1-4af818363df6/3845e114-9210-4306-bde1-4af818363df61.gif)
C#学习笔记FormClass
2014-2-22
FormClass
Representsawindowordialogboxthatmakesupanapplication'suserinterface.
Namespace:
System.Windows.Forms
Assembly:
System.Windows.Forms (inSystem.Windows.Forms.dll)
Syntax:
publicclassForm:
ContainerControl
Remarks:
A Form isarepresentationofanywindowdisplayedinyourapplication. The Form classcanbeusedtocreatestandard,tool,borderless,andfloatingwindows. Youcanalsousethe Form classtocreatemodalwindowssuchasadialogbox. Aspecialkindofform,themultiple-documentinterface(MDI)form,cancontainotherformscalledMDIchildforms. AnMDIformiscreatedbysettingthe IsMdiContainer propertyto true. MDIchildformsarecreatedbysettingthe MdiParent propertytotheMDIparentformthatwillcontainthechildform.
Usingthepropertiesavailableinthe Form class,youcandeterminetheappearance,size,color,andwindowmanagementfeaturesofthewindowordialogboxyouarecreating.The Text propertyallowsyoutospecifythecaptionofthewindowinthetitlebar. The Size and DesktopLocation propertiesallowyoutodefinethesizeandpositionofthewindowwhenitisdisplayed. Youcanusethe ForeColor colorpropertytochangethedefaultforegroundcolorofallcontrolsplacedontheform. The FormBorderStyle,MinimizeBox,and MaximizeBox propertiesallowyoutocontrolwhethertheformcanbeminimized,maximized,orresizedatruntime.
Inadditiontoproperties,youcanusethemethodsoftheclasstomanipulateaform. Forexample,youcanusethe ShowDialog methodtoshowaformasamodaldialogbox.Youcanusethe SetDesktopLocation methodtopositiontheformonthedesktop.
Theeventsofthe Form classallowyoutorespondtoactionsperformedontheform. Youcanusethe Activated eventtoperformoperationssuchasupdatingthedatadisplayedinthecontrolsoftheformwhentheformisactivated.
Youcanuseaformasthestartingclassinyourapplicationbyplacingamethodcalled Main intheclass. Inthe Main methodaddcodetocreateandshowtheform. Youwillalsoneedtoaddthe STAThread attributetothe Main methodinorderfortheformtorun. Whenthestartingformisclosed,theapplicationisalsoclosed.
Ifyousetthe Enabled propertyto false beforethe Form isvisible(forexample,setting Enabled tofalseintheMicrosoftVisualStudiodesigner),theminimize,maximize,close,andsystembuttonsremainenabled. Ifyouset Enabled to false afterthe Form isvisible(forexample,whentheLoadeventoccurs),thebuttonsaredisabled.
Properties:
1、AcceptButton:
publicIButtonControl型,GetsorsetsthebuttonontheformthatisclickedwhentheuserpressestheENTERkey.
Remarks:
ThispropertyenablesyoutodesignateadefaultactiontooccurwhentheuserpressestheENTERkeyinyourapplication. Thebuttonassignedtothispropertymustbean IButtonControl thatisonthecurrentformorlocatedwithinacontaineronthecurrentform.
YoucanusethispropertytoallowtheusertoquicklynavigateasimpleformbyallowingthemtosimplypresstheENTERkeywhentheyarefinishedinsteadofmanuallyclickingtheacceptbuttonwiththeirmouse.
TheacceptbuttonmightnotbeactivatedifthecurrentlyselectedcontrolontheforminterceptstheENTERkeyandprocessesit. Forexample,amultilinetextboxcontrolallowstheENTERkeytobepressedwhenitisselectedtoinsertanewlinecharacterinthecontrol.
2、ActiveForm:
publicstaticForm型,Getsthecurrentlyactiveformforthisapplication.or null ifthereisnoactiveform.
Remarks:
Youcanusethismethodtoobtainareferencetothecurrentlyactiveformtoperformactionsontheformoritscontrols.Ifyourapplicationisamultiple-documentinterface(MDI)application,usethe ActiveMdiChild propertytoobtainthecurrentlyactiveMDIchildform.
3、ActiveMdiChild:
publicForm型,Getsthecurrentlyactivemultiple-documentinterface(MDI)childwindow;or null iftherearecurrentlynochildwindowspresent.
Remarks:
YoucanusethismethodtodeterminewhetherthereareanyMDIchildformsopeninyourMDIapplication. YoucanalsousethismethodtoperformoperationsonanMDIchildwindowfromitsMDIparentformorfromanotherformthatisdisplayedinyourapplication.IfthecurrentlyactiveformisnotanMDIchildform,youcanusethe ActiveForm propertytoobtainareferencetoit.
4、AutoScaleBaseSize:
publicvirtualSize型,Getsorsetsthebasesizeusedforautoscalingoftheform.
Remarks:
Important:
Thismemberhasbeenretainedforbackwardcompatibility. Formoreinformationaboutautomaticscaling,see AutomaticScalinginWindowsForms.
Thevalueofthe AutoScaleBaseSize propertyisusedatform-displaytimetocomputethescalingfactorfortheform. Theautoscalingbasesizeisusedbytheformasabaselineforcomparisontothesystem'sfontsizetodeterminehowmuchtoscaletheformwhenautoscalingisused. Ifyouwanttodeterminethesizeaformwillautoscaletobasedonaspecificfont,usethe GetAutoScaleSize method.
Note:
Thevalueofthispropertyisusedwhentheformisinitiallycreated. Oncethepropertyisset,itcannotbechanged.
5、AutoScroll:
publicoverridebool型,Getsorsetsavalueindicatingwhethertheformenablesautoscrolling.Thedefaultis false.
Remarks:
Ifthispropertyissetto true,scrollbarsaredisplayedontheformifanycontrolsarelocatedoutsidetheform'sclientregion. Additionally,whenautoscrollingison,theclientareaoftheformautomaticallyscrollstomakethecontrolwithinputfocusvisible.
Youcanusethispropertytopreventusersfromlosingtheabilitytoviewcontrolswhentheirvideoresolutionsettingsaresettoalowresolution.
6、AutoSize:
publicoverridebool型,Getsorsetsthemodebywhichtheformautomaticallyresizesitself.
AutoSizeModeEnumeration
SpecifieshowacontrolwillbehavewhenitsAutoSizepropertyisenabled.
Namespace:
System.Windows.Forms.
Assembly:
System.Windows.Forms(insystem.windows.forms.dll)
Syntax:
publicenumAutoSizeMode
Membername
Description
GrowAndShrink
Thecontrolgrowsorshrinkstofititscontents.Thecontrolcannotberesizedmanually.
GrowOnly
ThecontrolgrowsasmuchasnecessarytofititscontentsbutdoesnotshrinksmallerthanthevalueofitsSizeproperty.Theformcanberesized,butconnotbemadesosmallthatanyofitscontainedcontrolsarehidden.
Remarks:
SettingtheGrowAndShrinkvalueproducesthesamebehaviorthatyougetforcontrolswiththeAutoSizepropertyenabledbutwhichhavenoAutoSizeModeproperty.TheMinimumSizeandMaximumSizepropertiesarerespected,butthecurrentvalueoftheSizepropertyisignored.
7、AutoSizeMode:
publicAutoSizeMode型,Getsorsetsthemodebywhichtheformautomaticallyresizesitself.Thedefaultis GrowOnly.
Remarks:
Settingthe AutoSizeMode propertytoanewvaluecausestheformtobelaidoutagain.
AformdoesnotautomaticallyresizeintheVisualStudioformsdesigner,regardlessofthevaluesofthe AutoSize and AutoSizeMode properties. Theformcorrectlyresizesitselfatruntimeaccordingtothevaluesofthesetwoproperties. Bycontrast,acustom UserControl automaticallyresizesitselfbothatdesigntimeandatruntime.
8、AutoValidate:
publicoverrideAutoValidate型,Getsorsetsavaluethatindicateswhethercontrolsinthiscontainerwillbeautomaticallyvalidatedwhenthefocuschanges.Thedefaultis Inherit.
9、BackColor:
publicoverrideColor型,Getsorsetsthebackgroundcolorforthecontrol.Thedefaultisthevalueofthe DefaultBackColor property.
10、CancelButton:
publicIButtonControl型,GetsorsetsthebuttoncontrolthatisclickedwhentheuserpressestheESCkey.
Remarks:
ThecancelbuttonforaformisthebuttoncontrolthatisclickedwhenevertheuserpressestheESCkey. ThebuttonassignedtothispropertymustbeanIButtonControl thatisonthecurrentformorlocatedwithinacontaineronthecurrentform.
ThispropertyallowsyoutodesignateadefaultactiontooccurwhentheuserpressestheESCkeyinyourapplication. YoucanusethispropertytoallowtheusertoquicklynavigateasimpleformbyallowingthemtosimplypresstheESCkeytocloseawindowwithoutcommittingchangesinsteadofmanuallyclickingthecancelbuttonwiththeirmouse.
CancelButton maynotworkifanothercontrolontheforminterceptstheESCkey. Forexample,ifyouhavea ComboBox openonyourform,ESCwillclosethe ComboBox insteadofclosingtheForm.
The IButtonControl objectassignedto CancelButton mustbevisibleontheform,orelsepressingtheESCkeywillhavenoeffect.
11、ClientSize:
publicSize型,Getsorsetsthesizeoftheclientareaoftheform.
Remarks:
Thesizeoftheclientareaoftheformisthesizeoftheformexcludingthebordersandthetitlebar. Theclientareaofaformistheareawithinaformwherecontrolscanbeplaced. Youcanusethispropertytogettheproperdimensionswhenperforminggraphicsoperationsorwhensizingandpositioningcontrolsontheform. Togetthesizeoftheentireform,usethe Size propertyorusetheindividualproperties Height and Width.
Note:
Youcannotcurrentlybindtothispropertyusingapplicationsettings. Formoreinformationonapplicationsettings,see ApplicationSettingsOverview.
12、ControlBox:
publicbool型,Getsorsetsavalueindicatingwhetheracontrolboxisdisplayedinthecaptionbaroftheform. Thedefaultis true.
Remarks:
Ifthe ControlBox propertyissetto true,thecontrolboxisdisplayedintheupper-leftcornerofthecaptionbar. Thecontrolboxiswheretheusercanclicktoaccessthesystemmenu.Ifyouset ControlBox to false,andalsosetthe Location property,the Size propertyofFormwillnotupdatetoreflectthatthenon-c