Programming Based on Events
370 likes | 549 Views
10. Programming Based on Events. C# Programming: From Problem Analysis to Program Design 3 rd Edition. Part II. MenuStrip Controls. Offers advantage of taking up minimal space Drag and drop MenuStrip object from toolbox to your form Icon representing MenuStrip placed in Component Tray
Programming Based on Events
E N D
Presentation Transcript
10 Programming Based on Events C# Programming: From Problem Analysis to Program Design 3rd Edition C# Programming: From Problem Analysis to Program Design
Part II C# Programming: From Problem Analysis to Program Design
MenuStrip Controls • Offers advantage of taking up minimal space • Drag and drop MenuStrip object from toolbox to your form • Icon representing MenuStrip placed in Component Tray • Select MenuStrip objectto set its properties • To add the text for a menu option, select the MenuStrip icon and then click in the upper-left corner of the form C# Programming: From Problem Analysis to Program Design
Adding Menus Drag MenuStrip control to form, then click here to display Menu structure Figure 10-9 First step to creating a menu C# Programming: From Problem Analysis to Program Design
MenuStrip Control Objects • Ampersand (&) is typed between the F and o for the Format option to make Alt+o shortcut for Format Figure 10-10 Creating a shortcut for a menu item C# Programming: From Problem Analysis to Program Design
MenuStrip Control Objects (continued) • To create separators, right-click on the text label (below the needed separator) • Select Insert Separator Figure 10-11 Adding a separator C# Programming: From Problem Analysis to Program Design
MenuStrip Control Objects (continued) Set the text to be displayed when the cursor is rested on top of the control Figure 10-12 Setting the Property for the ToolTip control C# Programming: From Problem Analysis to Program Design
Wire Methods to Menu Option Event • Set the Name property for each menu option • Do this first, then wire the event • Click events are registered by double-clicking on the Menu option • When the menu option is clicked, the event triggers, happens, or is fired C# Programming: From Problem Analysis to Program Design
Adding Predefined Standard Windows Dialog Boxes • Included as part of .NET • Dialog boxes that look like standard Windows dialog boxes • File Open, File Save, File Print, and File Print Preview • Format Font • Format Color dialogs C# Programming: From Problem Analysis to Program Design
Adding Predefined Standard Windows Dialog Boxes– Color Retrieves the current ForeColor property setting for the Label object privatevoid menuColor_Click(object sender, System.EventArgs e) { colorDialog1.Color = lblOutput.ForeColor; if (colorDialog1.ShowDialog( ) != DialogResult.Cancel ) { lblOutput.ForeColor = colorDialog1.Color; } } Checks to see if Cancel button clicked Set to selection made C# Programming: From Problem Analysis to Program Design
Adding Predefined Standard Windows Dialog Boxes– Color (continued) Figure 10-14 Color dialog box menu option C# Programming: From Problem Analysis to Program Design
Adding Predefined Standard Windows Dialog Boxes– Font privatevoid menuFont_Click (object sender, System.EventArgs e) { fontDialog1.Font = lblOutput.Font; if (fontDialog1.ShowDialog( ) != DialogResult.Cancel ) { lblOutput.Font = fontDialog1.Font ; } } Figure 10-15 Font dialog box menu option C# Programming: From Problem Analysis to Program Design
CheckBox Objects • Appear as small boxes • Allow users to make a yes/no or true/false selection • Checked property set to either true or false depending on whether a check mark appears or not • Default false value • CheckChanged( )– default event-handler method • Fired when CheckBox object states change • Can wire one event handler to multiple objects C# Programming: From Problem Analysis to Program Design
Wiring One Event Handler to Multiple Objects • Using Properties window, click on the Events Icon • Click the down arrow associated with that event • Select method to handle the event • Follow the same steps for other objects C# Programming: From Problem Analysis to Program Design
Wiring One Event Handler to Multiple Objects (continued) Figure 10-16 Wiring the event-handler method C# Programming: From Problem Analysis to Program Design
CheckBox Object Figure 10-17 ComputeCost_CheckedChanged( ) method raised C# Programming: From Problem Analysis to Program Design
GroupBox Objects • CheckBox objects may be grouped together for visual appearance • Can move or set properties that impact the entire group • A GroupBox control should be placed on the form before you add objects • GroupBox control adds functionality to RadioButton objects • Allow only one selection C# Programming: From Problem Analysis to Program Design
RadioButton Objects • Appear as small circles • Give users a choice between two or more options • Not appropriate to select more than one CheckBox objectwith RadioButton objects • Group RadioButton objects by placing them on a Panel or GroupBox control • Setting the Text property for the GroupBox adds a labeled heading over the group C# Programming: From Problem Analysis to Program Design
Adding RadioButton Objects Figure 10-18 GroupBox and RadioButton objects added C# Programming: From Problem Analysis to Program Design
RadioButton Objects (continued) • Turn selection on this.radInterm.Checked = true; • Raise a number of events, including Click( ) and CheckedChanged( ) events • Wire the event-handler methods for RadioButton objects, just like CheckBox C# Programming: From Problem Analysis to Program Design
Registering RadioButton Object Events • Register ComputeCost_CheckedChanged( ) method Figure 10-19 Wired Click event C# Programming: From Problem Analysis to Program Design
RadioButton Objects (continued) • ComputeCost_CheckedChanged( ) method if (this.radBeginner.Checked) { cost +=10; this.lblMsg.Text = "Beginner “ + “-- Extra $10 charge"; } else // more statements C# Programming: From Problem Analysis to Program Design
ComputeCost_CheckChanged( ) and Click( ) Events Raised Figure 10-20 ComputeCost_CheckedChanged( ) and Click( ) events raised C# Programming: From Problem Analysis to Program Design
Windows Presentation Foundation • WPF • Interface for Visual Studio 2010 was built using WPF • Vector-based and resolution-independent ->sharp graphics • As with WinForms, drag and drop controls from the Toolbox onto the window C# Programming: From Problem Analysis to Program Design
Windows Presentation Foundation (WPF) Figure 10-19 WPF design C# Programming: From Problem Analysis to Program Design
TabControl Controls • Sometime an application requires too many controls for a single screen • TabControl objectdisplays multiple tabs, like dividers in a notebook • Each separate tab can be clicked to display other options • Add a TabControl objectto the page by dragging the control from the Containersection of the Toolbox C# Programming: From Problem Analysis to Program Design
TabControl Controls (continued) Figure 10-21 Tabbed controlled application C# Programming: From Problem Analysis to Program Design
TabControl Controls (continued) Figure 10-22 TabControl object stretched to fill form C# Programming: From Problem Analysis to Program Design
TabControl Controls (continued) • TabPage property enables you to format individual tabs • Clicking the ellipsis beside the Collection value displays the TabPage Collection Editor Figure 10-23 TabControl's TabPage Collection Editor C# Programming: From Problem Analysis to Program Design
DinerGui Application Example Figure 10-24 Problem specification for DinerGUI example C# Programming: From Problem Analysis to Program Design
DinerGui Application Example (continued) C# Programming: From Problem Analysis to Program Design
DinerGui Application Example (continued) Figure 10-25 Prototype for DinerGUI example C# Programming: From Problem Analysis to Program Design
DinerGui Application Example (continued) Figure 10-26 Class diagrams C# Programming: From Problem Analysis to Program Design
DinerGui Application Example (continued) Figure 10-33 Message displayed from Current Order menu option C# Programming: From Problem Analysis to Program Design
Coding Standards • Follow a consistent naming standard for controls • Before you register events, such as button click events, name the associated control C# Programming: From Problem Analysis to Program Design
Chapter Summary • Delegates • Event-handling procedures • Registering an event • ListBox control for multiple selections • ComboBox versus ListBox objects C# Programming: From Problem Analysis to Program Design
Chapter Summary (continued) • Adding controls to save space • MenuStrip controls • TabControl • Use of GroupBox controls • RadioButton versus CheckBox objects C# Programming: From Problem Analysis to Program Design