Programming Based on Events - Department of … · ¾Contrast ComboBox to ListBox objects by adding...
Transcript of Programming Based on Events - Department of … · ¾Contrast ComboBox to ListBox objects by adding...
1
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
9 Programming Based on Events9
David McDonald, Ph.D.Director of Emerging Technologies
C# Programming: From Problem Analysis to Program Design 2nd Edition
Chapter Objectives
Create applications that use the ListBox control object to enable multiple selections from a single j p gcontrolContrast ComboBox to ListBox objects by adding both types of controls to an application Add Menu and TabControl control options to Window forms and program their event-handler methodsWire multiple RadioButton and CheckBox object
C# Programming: From Problem Analysis to Program Design
Wire multiple RadioButton and CheckBox object events to a single event-handler method
2
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
ListBox Control Objects
Displays list of items for single or multiple selections
√ Scroll bar is automatically added when total ynumber 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
C# Programming: From Problem Analysis to Program Design
√ The Items property used to set initial values
• Click on (Collections) to add items
Adding a ListBox Control Object
Add ListBox control, then
click on Items
C# Programming: From Problem Analysis to Program Design
property (Collection) to
type entries
Figure 9-2 String Collection Editor
3
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
ListBox Control Objects (continued)
Name property
√ Useful to set for program statements√ 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
C# Programming: From Problem Analysis to Program Design
√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
ListBox Control Objects (continued)
Register its event with the System.EventHandler delegateRegister its event with the System.EventHandler delegate
this.lstBoxEvents.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
Visual Studio adds event-handler method
private void listBox1_SelectedIndexChanged
(object sender System EventArgs e)
C# Programming: From Problem Analysis to Program Design
(object sender, System.EventArgs e)
{
}
4
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
ListBox Control Objects (continued)
To retrieve string data from ListBox use Text propertyText property
this.txtBoxResult.Text = this.lstBoxEvents.Text;
√Place in method body
C# Programming: From Problem Analysis to Program Design
√Place in method body
√When event fires, selection retrieved and stored in TextBox object
ListBox Control Objects (continued)
C# Programming: From Problem Analysis to Program Design
Figure 9-3 SelectedIndexChanged event fired
5
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Multiple Selections with a ListBoxSelectionMode Property has values of MultiSimple, MultiExtended, None, and One
√MultiSimple: use the spacebar and click the mouse√MultiSimple: use the spacebar and click the mouse
√MultiExtended can also use Ctrl key, Shift key, and arrow keys
foreach(string activity inlstBoxEvents.SelectedItems)
{
C# Programming: From Problem Analysis to Program Design
{
result += activity + " ";
}
this.txtBoxResult.Text = result;
ListBox Control Objects (continued)
C# Programming: From Problem Analysis to Program Design
Figure 9-4 Multiple selections within a ListBox object
6
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
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)
C# Programming: From Problem Analysis to Program Design
p ( j , y g )
{
lstBoxEvents.Items.Add(txtBoxNewAct.Text);
}
ListBoxExample
C# Programming: From Problem Analysis to Program Design
Figure 9-5 Add( ) method executed inside the buttonClick event
7
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
C# Programming: From Problem Analysis to Program Design
ListBox Control Properties
C# Programming: From Problem Analysis to Program Design
8
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
ListBox Control Methods
C# Programming: From Problem Analysis to Program Design
ListBox Control Methods (continued)
C# Programming: From Problem Analysis to Program Design
Note that ListBox control inherits members from Control class
9
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
ComboBox Controls
Extra TextBox object with ComboBox –User selects from list or types new value
C# Programming: From Problem Analysis to Program Design
Figure 9-6 ComboBox and ListBox objects
ComboBox Controls (continued)
Top line leftTop line left blank in ComboBox when DropDownStyle property is set to DropDown(default setting)
C# Programming: From Problem Analysis to Program Design
(default setting)
Figure 9-7 ComboBox list of choices
10
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
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
C# Programming: From Problem Analysis to Program Design
√ BUT, event is fired with each and EVERY keystroke
Programming Event HandlersSince 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
C# Programming: From Problem Analysis to Program Design
an array
√ SelectedIndices is a collection of indexes
11
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Programming Event Handlers
KeyPress( ) event-
handler method
fired with each
C# Programming: From Problem Analysis to Program Design
Figure 9-8 KeyPress and SelectedIndexChanged events fired
keystroke
MenuStrip Controls
Offers advantage of taking up minimal spaceDrag and drop MenuStrip object from toolbox to yourDrag and drop MenuStrip object from toolbox to your form√ Icon representing MenuStrip placed in Component
TraySelect MenuStrip object to set its propertiesTo add the text for a menu option, select the MenuStrip icon and then click in the upper left corner
C# Programming: From Problem Analysis to Program Design
MenuStrip icon and then click in the upper-left corner of the form
12
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
MenuStrip Controls (continued)
Drag MenuStrip control to form,
then click here to display Menu
structure
C# Programming: From Problem Analysis to Program Design
Figure 9-9 First step to creating a menu
MenuStrip Control Objects Ampersand (&) is typed between the F and o for the Format option to make Alt+o shortcut for Formatfor Format
C# Programming: From Problem Analysis to Program Design
Figure 9-10 Creating a shortcut for a menu item
13
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
MenuStrip Control Objects (continued)
To create separators, right-click on the text glabel (below the needed separator) Select Insert Separator
C# Programming: From Problem Analysis to Program Design
Figure 9-11 Adding a separator
MenuStrip Control Objects
Set the text to be displayedwhen the cursor is rested on top of the control
C# Programming: From Problem Analysis to Program Design
Figure 9-12 Setting the Property for the ToolTip control
14
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
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,
C# Programming: From Problem Analysis to Program Design
happens, or is fired
Adding Predefined Standard Windows Dialog Boxes
Included as part of .NETIncluded 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
C# Programming: From Problem Analysis to Program Design
√ Format Font
√ Format Color dialogs
15
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Adding Predefined Standard Windows Dialog Boxes – Color
private void menuColor_Click(object sender, System EventArgs e)
Retrieves the System.EventArgs e)
{colorDialog1.Color = lblOutput.ForeColor;if (colorDialog1.ShowDialog( ) !=
DialogResult.Cancel ){
current ForeColor property setting
for the Label object
Checks to see if C l b
C# Programming: From Problem Analysis to Program Design
{lblOutput.ForeColor = colorDialog1.Color;
}}
Cancel button clicked
Set to selection
made
CheckBox ObjectsAppear as small boxes
√ Allow users to make a yes/no or true/false selectionselection
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
C# Programming: From Problem Analysis to Program Design
√ Fired when CheckBox object states change
Can wire one event handler to multiple objects
16
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
Wiring One Event Handler to Multiple Objects
Using Properties window, click on the Events Icon Click the down arrow associated with that eventSelect method to handle the event
C# Programming: From Problem Analysis to Program Design
Select method to handle the eventFollow the same steps for other objects
CheckBox Object
C# Programming: From Problem Analysis to Program Design
Figure 9-17 ComputeCost_CheckedChanged( ) method raised
17
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
GroupBox Objects
CheckBox objects may be grouped together for visual appearanceappearance
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
C# Programming: From Problem Analysis to Program Design
p yobjects
√ Allow only one selection
RadioButton ObjectsAppear as small circles
Give users a choice between two or more options p
√ Not appropriate to select more than one CheckBox object with RadioButton objects
Group RadioButton objects by placing them on a Panel or GroupBox control
√
C# Programming: From Problem Analysis to Program Design
√ Setting the Text property for the GroupBox adds a labeled heading over the group
18
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
RadioButton Objects (example)
C# Programming: From Problem Analysis to Program Design
Figure 9-18 GroupBox and RadioButton objects added
RadioButton Objects (continued)
Turn selection on
this.radInterm.Checked = true;
Raise a number of events, including Click( ) and CheckedChanged( ) events
C# Programming: From Problem Analysis to Program Design
Wire the event-handler methods for RadioButton objects, just like CheckBox
19
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
RadioButton Objects (continued)Register ComputeCost_CheckedChanged( ) method( )
C# Programming: From Problem Analysis to Program Design
Figure 9-19 Wired Click event
RadioButton Objects (continued)ComputeCost_CheckedChanged( ) method
if (this.radBeginner.Checked){
cost +=10;this.lblMsg.Text =
"Beginner “ +
C# Programming: From Problem Analysis to Program Design
“-- Extra $10 charge";}else// more statements
20
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
ComputeCost_CheckChanged( ) and Click( ) Events Raised
C# Programming: From Problem Analysis to Program Design
Figure 9-20 ComputeCost_CheckedChanged( ) and Click( ) events raised
TabControl Controls
Sometime an application requires too l f i lmany controls for a single screen
TabControl object displays multiple tabs, like dividers in a notebookEach separate tab can be clicked to display other options
C# Programming: From Problem Analysis to Program Design
p y pAdd a TabControl object to the page by dragging the control from the Containersection of the Toolbox
21
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
TabControl Controls (continued)
C# Programming: From Problem Analysis to Program Design
Figure 9-21 Tabbed controlled application
TabControl Controls (continued)
C# Programming: From Problem Analysis to Program Design
Figure 9-22 TabControl object stretched to fill form
22
Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]
TabControl Controls (continued)TabPage property enables
t f tyou to format individual tabs Clicking the ellipsis beside the Collection value displays the TabPage
C# Programming: From Problem Analysis to Program Design
the TabPage Collection Editor