Programming Based on Events - PowerPoint PPT Presentation

1 / 51
About This Presentation
Title:

Programming Based on Events

Description:

Form Designer in Visual Studio did much of the work for you ... Click on (Collections) to add items. C# Programming: From Problem Analysis to Program Design ... – PowerPoint PPT presentation

Number of Views:97
Avg rating:3.0/5.0
Slides: 52
Provided by: course225
Category:

less

Transcript and Presenter's Notes

Title: Programming Based on Events


1
9
  • Programming Based on Events

2
Chapter Objectives
  • Explore event-handling procedures in C by
    writing and registering event-handler methods
  • Create applications that use the ListBox control
    object to enable multiple selections from a
    single control
  • Contrast ComboBox to ListBox objects by adding
    both types of controls to an application

3
Chapter Objectives (continued)
  • Add Menu and TabControl control options to Window
    forms and program their event-handler methods
  • Wire multiple RadioButton and CheckBox object
    events to a single event-handler method

4
Event Handling in C
  • Form Designer in Visual Studio did much of the
    work for you
  • Double-clicked on a Button control object during
    design
  • 1) Click event is registered as being of interest
  • 2) An event-handler method heading is generated
  • Two steps form event wiring process
  • Wire an event associate (identify) a method to
    handle its event

5
Event Handling in C (continued)
  • Code associates the methods with an event
  • this.button1.Click new System.EventHandler(this
    .button1_Click)
  • this.button2.Click new System.EventHandler(this
    .button2_Click)
  • button1.Click and button2.Click are methods
  • Keyword this is added to all code generated by
    Visual Studio to indicate the current instance of
    a class

6
ListBox Control Objects
  • Displays list of items for single or multiple
    selections
  • Scroll bar is automatically added when total
    number of items exceeds the number that can be
    displayed
  • Can add or remove items at design time or
    dynamically at run time
  • Includes number of properties and events
  • The Items property used to set initial values
  • Click on (Collections) to add items

7
Adding a ListBox Control Object
Add ListBox control, then click on Items property
(Collection) to type entries
Figure 9-2 String Collection Editor
8
ListBox Control Objects (continued)
  • Name property
  • Useful to set for program statements
  • Sorted property
  • Set to true to avoid having to type values in
    sorted order
  • Register an event for the ListBox
  • Might want to know when the item selection
    changes
  • Double-clicking on any control registers its
    default event for the control
  • SelectedIndexChanged default event for ListBox

9
ListBox Control Objects (continued)
  • Register its event with the System.EventHandler
  • this.lstBoxEvents.SelectedIndexChanged new
    System.EventHandler
    (this.listBox1_SelectedIndexChanged)
  • Visual Studio adds event-handler method
  • private void listBox1_SelectedIndexChanged
  • (object
    sender, System.EventArgs e)

10
ListBox Control Objects (continued)
  • To retrieve string data from ListBox use Text
    property
  • this.txtBoxResult.Text this.lstBoxEvents.Text
  • Place in method body
  • When event fires, selection retrieved and stored
    in TextBox object

11
ListBox Control Objects (continued)
Figure 9-3 SelectedIndexChanged event fired
12
Multiple Selections with a ListBox
  • SelectionMode Property has values of MultiSimple,
    MultiExtended, None, and One
  • MultiSimple use the spacebar and click the mouse
  • MultiExtended can also use Ctrl key, Shift key,
    and arrow keys
  • foreach(string activity in
    lstBoxEvents.SelectedItems)
  • result activity " "
  • this.txtBoxResult.Text result

13
ListBox Control Objects (continued)
Figure 9-4 Multiple selections within a ListBox
object
14
ListBox Control Objects (continued)
  • SelectedItem and SelectedItems return objects
  • Store numbers in the ListBox, once retrieved as
    objects, cast the object into an int or double
    for processing
  • Adding items to a ListBox at run time by using
    Add( ) method with the Items property
  • lstBoxEvents.Items.Add("string value to add")
  • private void btnNew_Click(object sender,
    System.EventArgs e)
  • lstBoxEvents.Items.Add(txtBoxNewAct.Text)

15
ListBoxExample
Figure 9-5 Add( ) method executed inside the
buttonClick event
16
(No Transcript)
17
ListBox Control Properties
18
ListBox Control Methods
19
ListBox Control Methods (continued)
Note that ListBox control inherits members from
Control class
20
ComboBox Controls
Extra TextBox object with ComboBox User selects
from list or types new value
Figure 9-6 ComboBox and ListBox objects
21
ComboBox Controls (continued)
Top line left blank in ComboBox when
DropDownStyle property is set to DropDown
(default setting)
Figure 9-7 ComboBox list of choices
22
Handling ComboBox Events
  • ComboBox only allows a single selection to be
    made
  • Default event-handler method SelectedIndexChanged
    ( )
  • Same as ListBox control object
  • Could register KeyPress( ) event-handler method
  • BUT, event is fired with each and EVERY keystroke

23
Programming Event Handlers
  • Since ListBox object allows multiple selections,
    Text property cannot be used
  • Text ONLY gets the first one selected
  • Use the SelectedItems, SelectedIndices, or Items
    to retrieve a collection of items selected
  • Zero-based structures
  • Access them as you would access an element from
    an array
  • SelectedIndices is a collection of indexes

24
Programming Event Handlers (continued)
KeyPress( ) event-handler method fired with each
keystroke
Figure 9-8 KeyPress and SelectedIndexChanged
events fired
25
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

26
MenuStrip Controls (continued)
Drag MenuStrip control to form, then click here
to display Menu structure
Figure 9-9 First step to creating a menu
27
MenuStrip Control Objects
  • Ampersand () is typed between the F and o for
    the Format option to make Alto shortcut for
    Format

Figure 9-10 Creating a shortcut for a menu item
28
MenuStrip Control Objects (continued)
  • To create separators, right-click on the text
    label (below the needed separator)
  • Select Insert Separator

Figure 9-11 Adding a separator
29
MenuStrip Control Objects (continued)
Set the text to be displayed when the cursor is
rested on top of the control
Figure 9-12 Setting the Property for the ToolTip
control
30
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

31
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

32
Adding Predefined Standard Windows Dialog Boxes
Color
Retrieves the current ForeColor property setting
for the Label object
  • private void 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
33
Adding Predefined Standard Windows Dialog Boxes
Color (continued)
Figure 9-14 Color dialog box menu option
34
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 9-15 Font dialog box menu option
35
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

36
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

37
Wiring One Event Handler to Multiple Objects
(continued)
Figure 9-16 Wiring the event-handler method
38
CheckBox Object
Figure 9-17 ComputeCost_CheckedChanged( ) method
raised
39
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

40
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

41
RadioButton Objects (continued)
Figure 9-18 GroupBox and RadioButton objects added
42
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

43
RadioButton Objects (continued)
  • Register ComputeCost_CheckedChanged( ) method

Figure 9-19 Wired Click event
44
RadioButton Objects (continued)
  • ComputeCost_CheckedChanged( ) method
  • if (this.radBeginner.Checked)
  • cost 10
  • this.lblMsg.Text
  • "Beginner
  • -- Extra 10 charge"
  • else
  • // more statements

45
ComputeCost_CheckChanged( ) and Click( ) Events
Raised
Figure 9-20 ComputeCost_CheckedChanged( ) and
Click( ) events raised
46
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

47
TabControl Controls (continued)
Figure 9-21 Tabbed controlled application
48
TabControl Controls (continued)
Figure 9-22 TabControl object stretched to fill
form
49
TabControl Controls (continued)
  • TabPage property enables you to format individual
    tabs
  • Clicking the ellipsis beside the Collection value
    displays the TabPage Collection Editor

50
Chapter Summary
  • Event-handling procedures
  • Registering an event
  • ListBox control for multiple selections
  • ComboBox versus ListBox objects

51
Chapter Summary (continued)
  • Adding controls to save space
  • MenuStrip controls
  • TabControl
  • Use of GroupBox controls
  • RadioButton versus CheckBox objects
Write a Comment
User Comments (0)
About PowerShow.com