10 Programming Based on Events C# Programming: From Problem Analysis to Program Design 3rd Edition C# Programming: From Problem Analysis to Program Design 1 Part II C# Programming: From Problem Analysis to Program Design 2 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 object to 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 3 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 4 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 5 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 6 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 7 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 8 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 9 Adding Predefined Standard Windows Dialog Boxes – Color Retrieves the private void menuColor_Click(object sender, current ForeColor System.EventArgs e) property setting { for the Label object colorDialog1.Color = lblOutput.ForeColor; if (colorDialog1.ShowDialog( ) != DialogResult.Cancel ) { Checks to see lblOutput.ForeColor = colorDialog1.Color; if Cancel } button clicked } Set to selection made C# Programming: From Problem Analysis to Program Design 10 Adding Predefined Standard Windows Dialog Boxes – Color (continued) Figure 10-14 Color dialog box menu option C# Programming: From Problem Analysis to Program Design 11 Adding Predefined Standard Windows Dialog Boxes – Font private void menuFont_Click (object sender, System.EventArgs e) { fontDialog1.Font = lblOutput.Font; if (fontDialog1.ShowDialog( ) != DialogResult.Cancel ) { lblOutput.Font = Figure 10-15 fontDialog1.Font ; Font dialog box menu option } } C# Programming: From Problem Analysis to Program Design 12 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 13 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 14 Wiring One Event Handler to Multiple Objects (continued) Figure 10-16 Wiring the event-handler method C# Programming: From Problem Analysis to Program Design 15 CheckBox Object Figure 10-17 ComputeCost_CheckedChanged( ) method raised C# Programming: From Problem Analysis to Program Design 16 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 17 RadioButton Objects • Appear as small circles • Give users a choice between two or more options – Not appropriate to select more than one CheckBox object with 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 18 Adding RadioButton Objects Figure 10-18 GroupBox and RadioButton objects added C# Programming: From Problem Analysis to Program Design 19 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 20 Registering RadioButton Object Events • Register ComputeCost_CheckedChanged( ) method Figure 10-19 Wired Click event C# Programming: From Problem Analysis to Program Design 21 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 22 ComputeCost_CheckChanged( ) and Click( ) Events Raised Figure 10-20 ComputeCost_CheckedChanged( ) and Click( ) events raised C# Programming: From Problem Analysis to Program Design 23 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 24 Windows Presentation Foundation (WPF) Figure 10-19 WPF design C# Programming: From Problem Analysis to Program Design 25 TabControl Controls • Sometime an application requires too many controls for a single screen • TabControl object displays multiple tabs, like dividers in a notebook • Each separate tab can be clicked to display other options • Add a TabControl object to the page by dragging the control from the Container section of the Toolbox C# Programming: From Problem Analysis to Program Design 26 TabControl Controls (continued) Figure 10-21 Tabbed controlled application C# Programming: From Problem Analysis to Program Design 27 TabControl Controls (continued) Figure 10-22 TabControl object stretched to fill form C# Programming: From Problem Analysis to Program Design 28 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 29 DinerGui Application Example Figure 10-24 Problem specification for DinerGUI example C# Programming: From Problem Analysis to Program Design 30 DinerGui Application Example (continued) C# Programming: From Problem Analysis to Program Design 31 DinerGui Application Example (continued) Figure 10-25 Prototype for DinerGUI example C# Programming: From Problem Analysis to Program Design 32 DinerGui Application Example (continued) Figure 10-26 Class diagrams C# Programming: From Problem Analysis to Program Design 33 DinerGui Application Example (continued) Figure 10-33 Message displayed from Current Order menu option C# Programming: From Problem Analysis to Program Design 34 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 35 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 36 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 37