Lecture 2 User Interface lecture.docx
- 文档编号:12758069
- 上传时间:2023-06-07
- 格式:DOCX
- 页数:36
- 大小:985.28KB
Lecture 2 User Interface lecture.docx
《Lecture 2 User Interface lecture.docx》由会员分享,可在线阅读,更多相关《Lecture 2 User Interface lecture.docx(36页珍藏版)》请在冰点文库上搜索。
Lecture2UserInterfacelecture
Ch2–UserInterface
Inthischapteryouwilllearntouseseveralmorecontroltypes:
textboxes,groupboxes,checkboxes,radiobuttons,andpictureboxes.
TextBoxes
1.Textboxcontrolisusedwhenyouwanttheusertotypesomeinput.
2.Referslidetoseesampleform.Infigure1,theformhastwotextboxes.
Figure1
3.Theusercanmovefromoneboxtothenext,makecorrections,cutandpasteifdesired,andclicktheDisplaybuttonwhenfinished.
4.Inyourprogramcode,youcanusetheTextpropertyofeachtextbox.
5.Example
nameLabel.Text=nameTextBox.Text;
6.Inthisexample,whatevertheuserentersintothetextboxisassignedtotheTextpropertyofnameLabel.
7.Ifyouwanttodisplaysometextinatextboxduringprogramexecution,assignaliteraltotheTextproperty:
messageTextBox.Text="Watson,comehere.";
8.YoucansettheTextAlignpropertyoftextboxestochangethealignmentoftextwithinthebox.
9.InthePropertieswindow,setthepropertytoLeft,Right,orCenter.Incode,youcansetthepropertyusingthesevalues:
HorizontalAlignment.Left
HorizontalAlignment.Right
HorizontalAlignment.Center
messageTextBox.TextAlign=HorizontalAlignment.Left;
MaskedTextBoxes
1.AspecializedformoftheTextBoxcontrolistheMaskedTextBox.
2.Youcanspecifytheformat(theMaskproperty)ofthedatarequiredoftheuser.
3.Forexample,youcanselectamaskforaZIPcode,adate,aphonenumber,orasocialsecuritynumber.
4.FigureshowstheInputMaskdialogbox,whereyoucanselectthemaskandeventryitout.
Figure2
5.Atruntime,theusercannotentercharactersthatdonotconformtothemask.
6.Forexample,thephonenumberandsocialsecuritynumbermasksdonotallowinputotherthannumericdigits.
7.Note:
Foradateortimemask,theusercanenteronlynumericdigitsbutmaypossiblyenteraninvalidvalue;forexample,amonthorhourgreaterthan12.
8.Themaskwillacceptanynumericdigits,whichcouldpossiblycauseyourprogramtogeneratearun-timeerror.YouwilllearntochecktheinputvaluesinChapter4.
RichTextBoxes
1.AnothervarietyoftextboxistheRichTextBoxcontrol,whichoffersseveralformattingfeatures(Figure3).
Figure3
2.Inaregulartextbox,allofthetextisformattedthesame,butinarichtextbox,theusercanapplycharacterandparagraphformattingtoselectedtext,muchlikeusingawordprocessor.
3.OnecommonuseforarichtextboxisfordisplayingURLaddresses.
4.Inaregulartextbox,theaddressappearsinthedefaultfontcolor,buttherichtextboxdisplaysitasalinkwhentheDetectUrlpropertyissettotrue.
5.Notethatitisnotanactivelink,butitdoeshavetheformattingtoshowtheURLasanaddress.
6.Youalsocanloadformattedtextintoarichtextboxfromafilestoredinrichtextformat(rtf).
7.UsetheLoadFilemethodoftherichtextbox.
8.InFigure3above,thefile“RichTextBoxes.rtf”isstoredinthebin\debugfolder,butyoucouldincludethecompletepathtoloadafilefromanotherlocation.
sampleRichTextBox.LoadFile("RichTextBoxes.rtf");
DisplayingTextonMultipleLines
1.Boththeregulartextboxandtherichtextboxhavepropertiesthatallowyoutodisplaytextonmultiplelines.
2.TheWordWrappropertydetermineswhetherthecontentsshouldwraptoasecondlineiftheydonotfitonasingleline.
3.Thepropertyissettotruebydefault.
4.BothcontrolsalsohaveaMultilineproperty,whichissettofalsebydefaultonatextboxandtruebydefaultonarichtextbox.
5.BothWordWrapandMultilinemustbesettotruefortexttowraptoasecondline.
6.Foraregulartextbox,youmustsetMultilinetotrueandthenadjusttheheighttoaccommodatemultiplelines.
7.IfMultilineisfalse(thedefault),atextboxdoesnothaveresizinghandlesforverticalresizing.
8.BeawarethatatextboxwillnotautomaticallyresizetodisplaymultiplelineseventhoughMultilineistrue;youmustmaketheheighttallenoughtodisplaythelines.
Figure4
9.YoucansettheTextpropertyofamultilinetextbox(orrichtextbox)toaverylongvalue;thevaluewillwraptofitinthewidthofthebox.
10.Youalsocanentermultiplelinesandchoosethelocationofthelinebreaks;thetechniquesdifferdependingonwhetheryousettheTextpropertyatdesigntimeorincode.
11.Atdesigntime,clickontheTextpropertyinthePropertieswindowandclickonthePropertiesbutton(thedownarrow);asmalleditingwindowpopsupwithinstructionstopressEnterattheendofeachlineandCtrl+Entertoacceptthetext(Figure2.5).
12.Incode,youcanuseaNewLinecharacter(Environment.NewLine)inthetextstringwhereyouwantthelinetobreak.
13.Joiningstringsoftextiscalledconcatenationandiscoveredinthesection“ConcatenatingText”laterinthischapter.
titleRichTextBox.Text="PamperYourself"+
Environment.NewLine+"AllYourFavoriteBooks";
GroupBoxes
1.Groupboxesareusedascontainersforothercontrols.
2.Usually,groupsofradiobuttonsorcheckboxesareplacedingroupboxes.
3.Usinggroupboxestogroupcontrolscanmakeyourformseasiertounderstandbyseparatingthecontrolsintologicalgroups.
4.YoucanfindtheGroupBoxcontrolintheContainerstabofthetoolbox.
5.Setagroupbox’sTextpropertytothewordsyouwanttoappearonthetopedgeofthebox.
6.ExampleNamesforGroupBoxes
colorGroupBox
styleGroupBox
7.Youonlyneedtochangethenameofagroupboxifyouplantorefertoitincode.
8.OnereasontouseitincodeistosettheEnabledpropertyofthegroupboxtofalse,whichdisablesallofthecontrolsinsidethebox.
CheckBoxes
1.Checkboxesallowtheusertoselect(ordeselect)anoption.
2.Inanygroupofcheckboxes,anynumbercanbeselected.
3.TheCheckedpropertyofacheckboxissettofalseifuncheckedortrueifchecked.
4.YoucanwriteaneventhandlerfortheCheckedChangedevent,whichexecuteswhentheuserclicksinthebox.
5.InChapter4,whenyoulearnaboutifstatements,youcantakeoneactionwhentheboxischeckedandanotheractionwhenitisunchecked.
6.UsetheTextpropertyofacheckboxforthetextyouwanttoappearnexttothebox.
7.ExampleNamesforCheckBoxes
boldCheckBox
italicCheckBox
RadioButtons
1.Useradiobuttonswhenonlyonebuttonofagroupmaybeselected.
2.Anyradiobuttonsthatyouplacedirectlyontheform(notinagroupbox)functionasagroup.
3.Agroupofradiobuttonsinsideagroupboxfunctiontogether.
4.Thebestmethodistofirstcreateagroupboxandthencreateeachradiobuttoninsidethegroupbox.
5.Whenyouneedseparatelistsofradiobuttonsfordifferentpurposes,youmustincludeeachlistinaseparategroupbox.
6.Youcanfindanexampleprogramlaterinthischapterthatdemonstratesusingtwogroupsofradiobuttons,oneforsettingthebackgroundcoloroftheformandasecondsetforselectingthecolorofthetextontheform.See“UsingRadioButtonsforSelectingColors.”
7.TheCheckedpropertyofaradiobuttonissettotrueifselectedortofalseifunselected.
8.Youcanwriteaneventhandlertoexecutewhentheuserselectsaradiobuttonusingthecontrol’sCheckedChangedevent.
9.InChapter4youwilllearntodetermineinyourcodewhetherornotabuttonisselected.Setaradiobutton’sTextpropertytothetextyouwanttoappearnexttothebutton.
10.ExampleNamesforRadioButtons
yellowRadioButton
blueRadioButton
PictureBoxes
1.APictureBoxcontrolcanholdanimage.
2.Youcansetapicturebox’sImagepropertytoagraphicfilewithanextensionof.bmp,.gif,.jpg,.jpeg,.png,.ico,.emf,or.wmf.Youfirstaddyourimagestotheproject’sresources;thenyoucanassigntheresourcetotheImagepropertyofaPictureBoxcontrol.
3.PlaceaPictureBoxcontrolonaformandthenselectitsImagepropertyinthePropertieswindow.ClickonthePropertiesbutton(Figure5)todisplayaSelectResourcedialogbox,whereyoucanselectimagesthatyouhavealreadyaddedoraddnewimages(Figure6).
Figure5
Figure6
4.ClickontheImportbuttonoftheSelectResourcedialogboxtoaddimages.
5.AnOpendialogboxappears(Figure7),whereyoucannavigatetoyourimagefiles.
Figure7
6.Apreviewoftheimageappearsinthepreviewbox.
7.Note:
Toaddfileswithan.icoextension,dropdowntheFileTypelistandselectAllFilesintheOpendialogbox.
8.Youcanuseanygraphicfile(withtheproperformat)thatyouhaveavailable.
9.YouwillfindmanygraphicfilesintheStudentData\ImagesfolderfromthetextbookWebsite:
10.PictureBoxcontrolshaveseveralusefulpropertiesthatyoucansetatdesigntimeorruntime.
11.Forexample,settheSizeModepropertytoStretchImagetomakethegraphicresizetofillthecontrol.
12.YoucansettheVisiblepropertytofalsetomakethepictureboxdisappear.
13.Forexample,tomakeapictureboxinvisibleatruntime,usethiscodestatement:
logoPictureBox.Visible=false;
AssigninganImagetoaPictureBox
1.ToassignagraphicfromtheResourcesfolderatruntime,yourefertotheprojectname(ChangePicturesinthefollowingexample),theResourcesfolderintheproject’sproperties,andthenameofthegraphicresource:
samplePictureBox.Image=ChangePictures.Properties.Resources.Water_Lilies;
ClearingaPictureBox
1.Sometimesyoumaywishtokeepthepictureboxvisiblebutremovethepicture.
2.Toaccomplishthis,settheImagepropertytonull,whichmeansempty.
samplePictureBox.Image=null;
Add
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Lecture User Interface