Tải bản đầy đủ - 0 (trang)
Chapter 13. Graphical User Interface Concepts: Part 1

Chapter 13. Graphical User Interface Concepts: Part 1

Tải bản đầy đủ - 0trang

Chapter13.GraphicalUserInterface

Concepts:Part1

...thewisestprophetsmakesureoftheeventfirst.

HoraceWalpole

...Theusershouldfeelincontrolofthecomputer;

nottheotherwayaround.Thisisachievedin

applicationsthatembodythreequalities:

responsiveness,permissiveness,andconsistency.

InsideMacintosh,Volume1AppleComputer,Inc.

1985

Allthebettertoseeyouwithmydear.

TheBigBadWolftoLittleRedRidingHood

OBJECTIVES

Inthischapteryouwilllearn:

Designprinciplesofgraphicaluserinterfaces(GUIs).

Howtocreategraphicaluserinterfaces.

Howtoprocesseventsthataregeneratedbyuser

interactionswithGUIcontrols.

Thenamespacesthatcontaintheclassesforgraphicaluser

interfacecontrolsandeventhandling.



HowtocreateandmanipulateButton,Label,RadioButton,

CheckBox,TextBox,PanelandNumericUpDowncontrols.

HowtoadddescriptiveToolTipstoGUIcontrols.

Howtoprocessmouseandkeyboardevents.



[Page597]



Outline

13.1 Introduction

13.2 WindowsForms

13.3 EventHandling















13.3.1 ASimpleEvent-DrivenGUI

13.3.2 AnotherLookattheVisualStudioGeneratedCode

13.3.3 DelegatesandtheEvent-HandlingMechanism

13.3.4 OtherWaystoCreateEventHandlers

13.3.5 LocatingEventInformation

13.4 ControlPropertiesandLayout

13.5 Labels,TextBoxesandButtons

13.6 GroupBoxesandPanels

13.7 CheckBoxesandRadioButtons

13.8 PictureBoxes

13.9 ToolTips



13.10 NumericUpDownControl

13.11 Mouse-EventHandling



13.12 Keyboard-EventHandling

13.13 Wrap-Up

Summary

Terminology

Self-ReviewExercises

AnswerstoSelf-ReviewExercises

Exercises



[Page597(continued)]



13.1.Introduction

Agraphicaluserinterface(GUI)allowsausertointeract

visuallywithaprogram.AGUI(pronounced"GOO-ee")givesa

programadistinctive"look"and"feel."Providingdifferent

applicationswithaconsistentsetofintuitiveuser-interface

componentsenablesuserstobecomeproductivewitheach

applicationfaster.



Look-and-FeelObservation13.1

Consistentuserinterfacesenableausertolearnnewapplicationsmore

quicklybecausetheapplicationshavethesame"look"and"feel."



AsanexampleofaGUI,considerFig.13.1,whichshowsan

InternetExplorerWebbrowserwindowcontainingvariousGUI

controls.Nearthetopofthewindow,thereisamenubar

containingthemenusFile,Edit,View,Favorites,ToolsandHelp.

Belowthemenubarisasetofbuttons,eachofwhichhasa

definedtaskinInternetExplorer,suchasgoingbacktothe

previouslyviewedWebpage,printingthecurrentpageor

refreshingthepage.Belowthesebuttonsliesacombobox,in

whichuserscantypethelocationsofWebsitesthattheywish

tovisit.Totheleftofthecomboboxisalabel(Address)that

indicatesthecombobox'spurpose(inthiscase,enteringthe

locationofaWebsite).Scrollbarsarelocatedattherightside

andbottomofthewindow.Usually,scrollbarsappearwhena

windowcontainsmoreinformationthancanbedisplayedinthe

window'sviewablearea.Scrollbarsenableausertoview

differentportionsofthewindow'scontents.Thesecontrolsform

auser-friendlyinterfacethroughwhichtheuserinteractswith

theInternetExplorerWebbrowser.



[Page598]

Figure13.1.GUIcontrolsinanInternetExplorer

window.



[Viewfullsizeimage]



GUIsarebuiltfromGUIcontrols(whicharesometimescalled

componentsorwidgetsshortforwindowgadgets).GUI

controlsareobjectsthatcandisplayinformationonthescreen

orenableuserstointeractwithanapplicationviathemouse,

keyboardorsomeotherformofinput(suchasvoice

commands).SeveralcommonGUIcontrolsarelistedinFig.

13.2inthesectionsthatfollowandinChapter14,wediscuss

eachoftheseindetail.Chapter14alsoexploresthefeatures

andpropertiesofadditionalGUIcontrols.

Figure13.2.SomebasicGUIcontrols.



[Page599]

Control



Description



Label



Displaysimagesoruneditabletext.



TextBox



Enablestheusertoenterdataviathekeyboard.Itcan

alsobeusedtodisplayeditableoruneditabletext.



Button



Triggersaneventwhenclickedwiththemouse.



CheckBox



Specifiesanoptionthatcanbeselected(checked)or

unselected(notchecked).



ComboBox



Providesadrop-downlistofitemsfromwhichtheuser

canmakeaselectioneitherbyclickinganiteminthe

listorbytypinginabox.



ListBox



Providesalistofitemsfromwhichtheusercanmakea

selectionbyclickinganiteminthelist.Multiple

elementscanbeselected.



Panel



Acontainerinwhichcontrolscanbeplacedand

organized.



NumericUpDown



Enablestheusertoselectfromarangeofinputvalues.







[Page599]



13.2.WindowsForms

WindowsFormsareusedtocreatetheGUIsforprograms.A

Formisagraphicalelementthatappearsonyourcomputer's

desktop;itcanbeadialog,awindoworanMDIwindow

(multipledocumentinterfacewindow)discussedinChapter

14,GraphicalUserInterfaceConcepts:Part2.Acomponentis

aninstanceofaclassthatimplementstheIComponent

interface,whichdefinesthebehaviorsthatcomponentsmust

implement,suchashowthecomponentisloaded.Acontrol,

suchasaButtonorLabel,hasagraphicalrepresentationat

runtime.Somecomponentslackgraphicalrepresentations(e.g.,

classTimerofnamespaceSystem.Windows.FormsseeChapter14).

Such,componentsarenotvisibleatruntime.

Figure13.3displaystheWindowsFormscontrolsand

componentsfromtheC#Toolbox.Thecontrolsandcomponents

areorganizedintocategoriesbyfunctionality.Selectingthe

categoryAllWindowsFormsatthetopoftheToolboxallowsyou

toviewallthecontrolsandcomponentsfromtheothertabsin

onelist(asshowninFig.13.3).Inthischapterandthenext,

wediscussmanyofthesecontrolsandcomponents.Toadda

controlorcomponenttoaForm,selectthatcontrolcomponentor

fromtheToolboxanddragitontheForm.Todeselectacontrolor

component,selectthePointeritemintheToolbox(theiconatthe

topofthelist).WhenthePointeritemisselected,youcannot

accidentallyaddanewcontroltotheForm.



Figure13.3.Componentsandcontrolsfor

WindowsForms.

(Thisitemisdisplayedonpage600intheprintversion)



[Viewfullsizeimage]



Whenthereareseveralwindowsonthescreen,theactive

windowisthefrontmostandhasahighlightedtitlebartypically

darkerbluethantheotherwindowsonthescreen.Awindow

becomestheactivewindowwhentheuserclickssomewhere

insideit.Theactivewindowissaidto"havethefocus."For

example,inVisualStudiotheactivewindowistheToolboxwhen

youareselectinganitemfromit,orthePropertieswindowwhen

youareeditingacontrol'sproperties.

AFormisacontainerforcontrolsandcomponents.Whenyou

dragacontrolorcomponentfromtheToolboxontheForm,Visual

Studiogeneratescodethatinstantiatestheobjectandsetsits

basicproperties.Thiscodeisupdatedwhenthecontrolor

component'spropertiesaremodifiedintheIDE.Ifacontrolor

componentisremovedfromtheForm,thegeneratedcodefor



thatcontrolisdeleted.ThegeneratedcodeisplacedbytheIDE

inaseparatefileusingpartialclasses.Althoughwecouldwrite

thiscodeourselves,itismucheasiertocreateandmodify

controlsandcomponentsusingtheToolboxandProperties

windowsandallowVisualStudiotohandlethedetails.We

introducedvisualprogrammingconceptsinChapter2.Inthis

chapterandthenext,weusevisualprogrammingtobuildmore

substantialGUIs.



[Page600]

Eachcontrolorcomponentwepresentinthischapterislocated

innamespaceSystem.Windows.Forms.TocreateaWindows

application,yougenerallycreateaWindowsForm,setits

properties,addcontrolstotheForm,settheirpropertiesand

implementeventhandlers(methods)thatrespondtoevents

generatedbythecontrols.Figure13.4listscommonForm

properties,methodsandevents.

Figure13.4.CommonFormproperties,methodsand

events.



[Page601]

Formproperties,



methodsand

events



Description



CommonProperties

AcceptButton



ButtonthatisclickedwhenEnterispressed.



AutoScroll



Booleanvaluethatallowsordisallowsscrollbars



whenneeded.

CancelButton



ButtonthatisclickedwhentheEscapekeyis



pressed.

FormBorderStyle



BorderstylefortheForm(e.g.,none,single,



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Chapter 13. Graphical User Interface Concepts: Part 1

Tải bản đầy đủ ngay(0 tr)

×