Download presentation
Presentation is loading. Please wait.
1
C# Programming: From Problem Analysis to Program Design1 Programming Based on Events C# Programming: From Problem Analysis to Program Design 3 rd Edition 10
2
Part II C# Programming: From Problem Analysis to Program Design2
3
3 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
4
C# Programming: From Problem Analysis to Program Design4 Adding Menus Drag MenuStrip control to form, then click here to display Menu structure Figure 10-9 First step to creating a menu
5
C# Programming: From Problem Analysis to Program Design5 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
6
C# Programming: From Problem Analysis to Program Design6 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
7
C# Programming: From Problem Analysis to Program Design7 MenuStrip Control Objects ( continued ) Figure 10-12 Setting the Property for the ToolTip control Set the text to be displayed when the cursor is rested on top of the control
8
C# Programming: From Problem Analysis to Program Design8 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
9
C# Programming: From Problem Analysis to Program Design9 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
10
C# Programming: From Problem Analysis to Program Design10 Adding Predefined Standard Windows Dialog Boxes – Color private void menuColor_Click(object sender, System.EventArgs e) { colorDialog1.Color = lblOutput.ForeColor; if (colorDialog1.ShowDialog( ) != DialogResult.Cancel ) { lblOutput.ForeColor = colorDialog1.Color; } Retrieves the current ForeColor property setting for the Label object Checks to see if Cancel button clicked Set to selection made
11
C# Programming: From Problem Analysis to Program Design11 Adding Predefined Standard Windows Dialog Boxes – Color ( continued ) Figure 10-14 Color dialog box menu option
12
C# Programming: From Problem Analysis to Program Design12 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 = fontDialog1.Font ; } Figure 10-15 Font dialog box menu option
13
C# Programming: From Problem Analysis to Program Design13 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
14
C# Programming: From Problem Analysis to Program Design14 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
15
C# Programming: From Problem Analysis to Program Design15 Wiring One Event Handler to Multiple Objects ( continued ) Figure 10-16 Wiring the event-handler method
16
C# Programming: From Problem Analysis to Program Design16 CheckBox Object Figure 10-17 ComputeCost_CheckedChanged( ) method raised
17
C# Programming: From Problem Analysis to Program Design17 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
18
C# Programming: From Problem Analysis to Program Design18 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
19
C# Programming: From Problem Analysis to Program Design19 Adding RadioButton Objects Figure 10-18 GroupBox and RadioButton objects added
20
C# Programming: From Problem Analysis to Program Design20 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
21
C# Programming: From Problem Analysis to Program Design21 Registering RadioButton Object Events Register ComputeCost_CheckedChanged( ) method Figure 10-19 Wired Click event
22
C# Programming: From Problem Analysis to Program Design22 RadioButton Objects ( continued ) ComputeCost_CheckedChanged( ) method if (this.radBeginner.Checked) { cost +=10; this.lblMsg.Text = "Beginner “ + “-- Extra $10 charge"; } else // more statements
23
C# Programming: From Problem Analysis to Program Design23 ComputeCost_CheckChanged( ) and Click( ) Events Raised Figure 10-20 ComputeCost_CheckedChanged( ) and Click( ) events raised
24
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 Design24
25
Windows Presentation Foundation (WPF) C# Programming: From Problem Analysis to Program Design25 Figure 10-19 WPF design
26
C# Programming: From Problem Analysis to Program Design26 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
27
C# Programming: From Problem Analysis to Program Design27 TabControl Controls ( continued ) Figure 10-21 Tabbed controlled application
28
C# Programming: From Problem Analysis to Program Design28 TabControl Controls ( continued ) Figure 10-22 TabControl object stretched to fill form
29
C# Programming: From Problem Analysis to Program Design29 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
30
C# Programming: From Problem Analysis to Program Design30 DinerGui Application Example Figure 10-24 Problem specification for DinerGUI example
31
C# Programming: From Problem Analysis to Program Design31 DinerGui Application Example ( continued )
32
C# Programming: From Problem Analysis to Program Design32 DinerGui Application Example ( continued ) Figure 10-25 Prototype for DinerGUI example
33
C# Programming: From Problem Analysis to Program Design33 DinerGui Application Example ( continued ) Figure 10-26 Class diagrams
34
C# Programming: From Problem Analysis to Program Design34 DinerGui Application Example ( continued ) Figure 10-33 Message displayed from Current Order menu option
35
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 Design35
36
C# Programming: From Problem Analysis to Program Design36 Chapter Summary Delegates Event-handling procedures –Registering an event ListBox control for multiple selections ComboBox versus ListBox objects
37
C# Programming: From Problem Analysis to Program Design37 Chapter Summary ( continued ) Adding controls to save space –MenuStrip controls –TabControl Use of GroupBox controls RadioButton versus CheckBox objects
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.