150 likes | 163 Views
Child Window Controls are created by a parent window and used for I/O tasks. They have a consistent look and feel with other applications and can be customized. They allow users to display and select data in standard ways. Learn about the different types of Child Window Controls in Windows and how to create and manipulate them.
E N D
WindowsControls ChildWindowControls •Windowscreatedbyaparentwindow •Anapplicationusestheminconjunctionwithparent •NormallyusedforsimpleI/Otasks •Havealookandfeelconsistentwithotherapplication Windows •Properties,appearance,behaviordeterminedbypredefinedControlClassdefinitions –Butbehaviorcanbecustomized –EasytosetthemupascommonWindowsobjects •buttons,scrollbars,etc. •CanalsodefinecustomChildWindowControls
•Allowusertodisplay/selectdatainstandardways •WindowsEnvironmentdoesmostofworkin: –painting/updatingaControl'sscreenarea –determiningwhatuserisdoing •Candothe"dirtywork"forthemainwindow •Arethe"workingcomponents"ofDialogBoxes •WindowsOScontainseachcontrol's“WndProc” –somessagestocontrolsareprocessedinpredefinedways •Parent/childrelationshipwithmainwindow –Canhavehierarchiesofchildwindows –Parentandchildcommunicatebysending/receivingmessages •HavebeenpartofWindowssincethefirstversions •Rosterhasgrownfromsixbasiconestoanassortmentof 20+richandvariedcontrols Some.NETControlClasses •Button •Label(Static) •GroupBox •Panel •CheckBox •RadioButton •HScrollBar •VScrollBar •TextBox(Edit) •PictureBox •ListBox •ComboBox •StatusBar •TabControl •ToolBar
•ToolTip •CheckedListBox •DataGrid •DataGridTextBox •DateTimePicker •LinkLabel •ListView •MonthCalendar •NumericUpDown--spinnerbuttons •ProgressBar •PropertyGrid •RichTextBox •TrackBar •TreeView •Others CreatingaControlin.NET •Tocreateacontrolandmakeitappearonaform: 1.DeclareandInstantiateaControlclassobject ButtonmyButton; myButton=newButton(); 2.InitializetheControlobjectbysettingitsproperties myButton.Location=newPoint(10,10); myButton.Text=“ClickMe”; myButton.BackColor=Color.Red; •//etc. 3. AttachtheControltotheForm(addtoparent’scollectionofControls)…
AttachingControlstoaParentForm •AssumewewanttoaddmyButtonandmyLabelcontrolsto“this”Form •Threewaysofdoingit(assumewe’veinstantiatedthecontrolsmyButtonandmyLabel): 1. myButton.Parent=this; myLabel.Parent=this; 2. this.Controls.Add(myButton); this.Controls.Add(myLabel); 3. this.Controls.AddRange(newControl[]{myButton,myLabel}); •Controlsproperty:thecollectionofcontrolsattachedtotheform •#3isdoneautomaticallybytheVisualStudioDesignerwhenyou “drag”controlsontotheform SomeControlProperties/Methods •Commonpropertiesandmethods –DerivefromclassControl –Textproperty •Specifiesthetextthatappearsonacontrol –TextAlignproperty •Alignmentoftextinsidecontrol –Focus()method •Transferstheinputfocustoacontrol •Thecontrolbecomestheactivecontrol –TabIndexproperty •Determinesorderinwhichcontrolsaregiventhefocuswhenusertabs •AutomaticallysetbyVisualStudio.NETDesigner –Enabledproperty •Indicateacontrol’saccessibilityandusability
•Visibleproperty –Hidecontrolfromuser •OrusemethodHide() •Show()todisplayagain •AnchorandDockproperties –Anchoringcontroltospecificlocation •Constantdistancefromspecifiedlocationwhenparentisresized •DefaultinDesignerisTop-Left –Unanchoredcontrolmovesrelativetoformerposition –Dockingallowscontroltospreaditselfalonganentireside –Bothoptionsrefertotheparentcontainer •Sizeproperty •BackColor,ForeColorproperties •Image,ImageAlign,BackgroundImageproperties ControlPropertiesandLayout BeforeresizeAfterresize Constantdistance toleftandtop sides Anchoringdemonstration.
ControlPropertiesandLayout Darkenedbar indicatestowhich wallcontrolis anchored Clickdown-arrow inAnchorproperty todisplay anchoringwindow ManipulatingtheAnchorpropertyofacontrol. ControlLayout Controlexpandsalong topportionoftheform Dockingdemonstration.
ControlEvents •AllControlsderivefrom System.Windows.Forms.Control –Allinherit50+publicEvents –Somecommonones: EventEventargument ClickEventArgs DoubleClickEventArgs ControlAddedControlEventArgs ControlRemovedControlEventArgs EnterEventArgs LeaveEventArgs MoveEventArgs PaintPaintEventArgs ResizeEventArgs SizeChangedEventArgs AllothermouseeventsMouseEventArgs •EventhandlingdoneaswithFormevents AddingaButtonClickEventHandler •TheButtonClickEventDelegateisEventHandler() myButton.Click+=newEventHandler(myButton_Click); … privatevoidmyButton_Click(objectsender,System.EventArgse) { //Addhandlercodehere } •ThiscodeisinsertedautomaticallywhenyouusetheVisualStudio DesignerPropertiesWindowtoaddaClickeventhandler –OrdoubleclickontheControlinVisualStudioDesigner
ButtonControls •Rectangularobjects,oftenwithlabels •Intendedtotriggeranimmediateaction –Actionistriggeredbyclickingmouseonbutton –Orpressingspacebarifithastheinputfocus •SomeimportantButtonproperties: –Location,Size,BackColor,ForeColor,Cursor,Name,Text,TextAlign,Font,Image,ImageAlign,BackgroundImage,TabIndex, –Lotsofothers LabelControls •Controlsdesignedforthedisplayofstatictext –CalledStaticcontrolsinWin32 –Usercan’tchangethetext •Canbechangedincode •Canalsodisplaygraphics •HavemanyofthesamePropertiesasButtons •Canrespondtoevents,butnotreallymeanttodothat
Button-LabelExampleProgram •FormhasaButtoncontrolwithText:“ClickMe” •FormhasaLabelcontrolthatdisplays“HelloWorld”whenbuttonisclicked –Inresponsetothebutton’sClickevent •CanbepreparedmanuallyfromVisualStudio –Programmermustwritecodetoinstantiatethecontrols,attachthemtotheparentform,setupalltheirproperties,andaddtheButtonClickeventhandler •EasiertousetheVisualStudioDesigner –Dragabuttonandlabelcontrolfromthetoolboxtotheform •Controlsareautomaticallyinstantiated&“attached”totheform –ChangethePropertiesofeachinthePropertywindowofeach –AddtheButtonClickhandlerbydoubleclickingonthebutton •OrusingtheButton’sPropertieswindow(lightningbolt) –Addthefollowingcodeintheskeletonhandler label1.Text=“HelloWorld”; ButtonswithImages •ButtonclasshasanImageProperty –Setthatpropertytodisplayanimageonbackgroundofthebutton •CanbeusedinconjunctionwithTextProperty –Textdisplayedontopoftheimage •Makesureimagefitsinthebutton –Canuseimg.GetThumbNailImage(…)toresizetheimage •Arguments:intw,inth,Image.GetThumbnailImageAbortgt,IntPtrp •Lasttwocanspecifyacallbackfunction&data–usuallysettonulland (IntPtr)0,respectively •Returnsthethumbnailimage –Thiscanbeusedasageneralimageresizingfunction –Alternatively,makethebuttonbethesizeoftheimage •Changethebutton’sSizeproperty •ExampleProgram:Button-Image –DoessameasButton-Label,butnowbuttonhasanimageonit
GroupBoxandPanelControls •ArrangecomponentsonaGUI –GroupBoxescandisplayacaption •Almostalwayscontainothercontrols –RadioButtonsorCheckBoxesareverycommon –Onlyoneactiveatatime •Textpropertydeterminesitscaption –Panelsareusedtogroupothercontrolsagainstabackground •Usefulwhenyouneedacontrolthatdoesn’tdomuch •Ifcontentsofpaneltakeupmorespacethanpanelitself,attachedscrollbarscanautomaticallyappear –SousercanviewadditionalcontrolsinsidethePanel GroupBoxControlProperties GroupBoxDescription Properties Common PropertiesControlsThecontrolsthattheGroupBoxcontains.TextTextdisplayedonthetopportionofthe GroupBox(itscaption).
PanelControlProperties Panel Properties Description Common Properties AutoScroll WhetherscrollbarsappearwhenthePanelistoosmalltoholditscontrols.Defaultisfalse. BorderStyle BorderofthePanel(defaultNone; FixedSingle). Fixed3D otheroptionsare and Controls ThecontrolsthatthePanelcontains. Panelproperties. Panels Controls panel scrollbars CreatingaPanelwithscrollbars. panel insidepanel
GroupBox-PanelExample Program •OrganizesonegroupofbuttonsinaGroupBox –GroupBoxislabeled •OrganizesanothergroupofbuttonsinaPanelthatistoosmalltoviewitsbuttons –AutoScrollPropertyisset=>ScrollbarsautomaticallyappeartopermitusertoviewallthebuttonsinsidethePanel •Clickinganybuttoncausesalabelcontroltoindicatewhichbuttonwasclicked ScrollBars •UsedeverywhereinGUIs •Twopurposes: –Toshift(“scroll”)thevisibleareaofaform/control •Scrollbarisattachedtothecontrol/form •Setparentform/control’sAutoScrollPropertytotrue –Tovaryaparameter •Standalonescrollbar •ScrollbarPropertiesthatcanberead/modified: –SizeandLocationonparentcontrol/form –Range:MaximumandMinimumthumbposition –CurrentValueofthumbposition –Changevalues •SmallChange:Valuechangewhenuserclicksonendarrows •LargeChange:valuechangewhenuserclicksonareabetweenendarrowsandthumb
ScrollBarEvents •TwoeventsraisedbyScrollBarcontrols –ValueChanged--Data:EventArgs •RaisedwhenValuepropertyhaschanged,eitherbyaScrolleventorprogrammatically –Scroll--Data:ScrollEventArgs •Raisedwhenscrollbarthumbhasbeenmoved,eitherbymouseorkeyboard •Providesinformationabouttheevent,includingthenewvalueandtypeofevent •ScrollEventprovidesmoreinformationthanValueChanged •SomeScrollEventArgsProperties: –IntValue –ScrollEventTypeType »EnumerationMembers:SmallDecrement(LorTarrow),SmallIncrement(RorB),LargeDecrement(LorTareas),LargeInccrement(RorB),ThumbTrack(Thumbdown)ThumbPosition(thumbup),EndScroll(scrolloperationdone),Others Scroll-ImageExample •Addstandalonehorizontalandverticalscrollbarstomainform –Positionhorizontalonealongbottomofform –Verticaloneonrightside,leavingspaceonrightfor2labelcontrols •ControlthepositionofanImagewiththescrollbars •Labelcontrolsshowcurrentposition(x,y)ofimage •Events: –Paint:drawimageinitsnewposition –Scrollofhorizontalscrollbar:setnewxvalueofimageposition,changelabel1’stexttocurrentscrollbarValue,&repaint –Scrollofverticalscrollbar:setnewyvalueofimageposition,changelabel2’stexttocurrentscrollbarValue,&repaint –Resize:repositionscrollbarsandresettheirMaximumvalues
RadioButtons&CheckBoxes •Botharepredefined“state”buttonsthatallowusertoselectordeselectagivenoption –Canbesetto“on”or“off”(selected/unselected)state –Foreach,theCheckedPropertyissettofalseifbuttonisunselectedandtrueifselected –IfAutoCheckpropertyistrue,statetoggleswhenuserclicks •RadioButtons –Almostalwaysusedinagroupboxfromwhichonlyonebuttoninthegroupcanbeselectedatatime •Mutuallyexclusiveoptions •Theyareallchildrenofthegroupbox…whichisachildoftheform –Displayedaslittlecircles •Selectedcirclehasadotinside •CheckBoxes –Ifenclosedinagroupbox,anynumberofthemcanbeselected –Displayedaslittleboxes •Selectedboxeshavecheckmarksinthem SomeCheckBoxPropertiesand Events CheckBoxeventsandproperties Description/DelegateandEventArguments CommonProperties Checked WhetherornottheCheckBoxhasbeenchecked. CheckState WhethertheCheckboxischecked(containsablackcheckmark)orunchecked(blank).AnenumerationwithvaluesChecked,UncheckedorIndeterminate. Text TextdisplayedtotherightoftheCheckBox(calledthelabel). CommonEvents (DelegateEventHandler,eventarguments EventArgs) CheckedChanged RaisedeverytimetheCheckboxiseithercheckedorunchecked.Defaulteventwhenthiscontrolisdoubleclickedinthedesigner. CheckStateChanged RaisedwhentheCheckStatepropertychanges.
SomeRadioButtonProperties& Events RadioButtonDescription/DelegateandEventpropertiesandArguments eventsCommonProperties CheckedWhethertheRadioButtonischecked. TextTextdisplayedtotherightofthe CommonEvents(DelegateEventHandler,eventargumentsEventArgs) ClickRaisedwhenuserclicksthecontrol. CheckedChangedRaisedeverytimetheRadioButtonischeckedorunchecked.Defaulteventwhenthiscontrolisdoubleclickedinthedesigner. RadioButton(calledthelabel). Radio-CheckExampleProgram •Drawsopenorfilledrectanglesofdifferent colors •A‘ColorSelection’groupboxcontainingradio buttonsallowsusertoselectacolor •A‘FillRectangle’checkboxdetermineswhethertherectangleisfilledornot