Microsoft Visual C#.NET: From Problem Analysis to Program Design1 Chapter 9 Programming Based on Events Microsoft Visual C#.NET: From Problem Analysis.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Select Case Statements and Selection Input.
Advertisements

Working with Intrinsic Controls and ActiveX Controls
Using Macros and Visual Basic for Applications (VBA) with Excel
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
C# Programming: From Problem Analysis to Program Design1 9 Programming Based on Events.
C# Programming: From Problem Analysis to Program Design1 Programming Based on Events C# Programming: From Problem Analysis to Program Design 3 rd Edition.
Programming Based on Events
Graphical User Interface (GUI) A GUI allows user to interact with a program visually. GUIs are built from GUI components. A GUI component is an object.
ListBoxes The list box control allows the user to view and select from multiple items in a list. CheckedListBox control extends a list box by including.
Group Boxes and Panels Arrange components on a GUI Buttons and etc. can be placed inside a group box or panel. All these buttons move together when the.
Programming Based on Events
C# Programming: From Problem Analysis to Program Design1 Introduction to Windows Programming C# Programming: From Problem Analysis to Program Design 3.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
BIM313 – Advanced Programming Simple Controls 1. Contents Traditional Controls – Labels, Text Boxes, Buttons, Check Boxes, List Boxes, Combo Boxes Advanced.
Database-Driven Web Sites, Second Edition1 Chapter 8 Processing ASP.NET Web Forms and Working With Server Controls.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
1 Graphical User Interfaces Part 2 Outline ListBoxes and CheckedListBoxes ListBoxes CheckedListBoxes ComboBoxes.
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
Chapter 12: Using Controls. Examining the IDE’s Automatically Generated Code A new Windows Forms project has been started and given the name FormWithALabelAndAButton.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Typing Application Introducing Keyboard Events, Menus, Dialogs and the Dictionary.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter 9 - VB.Net by Schneider1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes, Combo Boxes, and the File-Opening Control The List Box Control.
ListBox, ComboBox, Menu Chapter 5.4, ComboBox Control: Properties & Methods u Combines TextBox features with a short drop- down list  cboOne.AddItem(string)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 13 – Graphical User Interfaces Part 2 Outline.
Graphical User Interface Concepts - Part 2 Session 09 Mata kuliah: M0874 – Programming II Tahun: 2010.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Program Design and Coding
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2010 CHAPTER THREE Program Design and Coding.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 - Graphical User Interface Concepts: Part.
Controls in C++/CLI (2) CheckBox RadioButton GroupBox ListBox.
1 Chapter Eleven Handling Events. 2 Objectives Learn about delegates How to create composed delegates How to handle events How to use the built-in EventHandler.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Chapter Ten Using Controls. 2 Objectives Learn about Controls How to create a Form containing Labels How to set a Label’s Font How to add Color to a.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Microsoft Visual Basic 2012 CHAPTER ELEVEN Multiple Classes and Inheritance.
Object-Oriented Application Development Using VB.NET 1 Chapter 10 VB.NET GUI Components Overview.
Slide 1 Using Menu Bar & Common Dialog Boxes. Slide 2 Setting Up the Main Items v First open the form on which you want the menu located v Then start.
Lesson 1 - Understanding the Word Window and Creating a New Document
Controls Part 2. DateTimePicker Control Used for representing Date/Time information and take it as input from user. Date information is automatically.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed.
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Chapter 2 Introduction to Visual Basic Programming Visual Basic.NET.
Unit 6 Repetition Processing Instructor: Brent Presley.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code (loop body) that may be executed several times. Fixed-count (definite) loops repeat.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 11 Creating Web Applications and Writing Data to a Database.
Chapter 9 Programming Based on Events
INF230 Basics in C# Programming
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Computing with C# and the .NET Framework
Programming Based on Events
C# Programming: From Problem Analysis to Program Design
3.01 Apply Controls Associated With Visual Studio Form
Chapter Eleven Handling Events.
3.01 Apply Controls Associated With Visual Studio Form
Using Procedures and Exception Handling
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Programming Based on Events
Presentation transcript:

Microsoft Visual C#.NET: From Problem Analysis to Program Design1 Chapter 9 Programming Based on Events Microsoft Visual C#.NET: From Problem Analysis to Program Design

2 Chapter Objectives Define, create, and use delegates and examine their relationship to events 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

Microsoft Visual C#.NET: From Problem Analysis to Program Design3 Chapter Objectives ( continued ) Add Menu control options to Window forms and program their event-handler methods Wire multiple RadioButton and CheckBox object events to a single event-handler method Work through a programming example that illustrates the chapter’s concepts

Microsoft Visual C#.NET: From Problem Analysis to Program Design4 Delegates Delegates store references (addresses) to methods—as opposed to storing actual data –Delegates form the foundation for events in C# Declaration for a delegate looks more like a method declaration than a class definition –Except—delegate declaration has no body –Declaration begins with the keyword delegate Declaration ends with a parenthesized list of parameters Unlike a method, the return type of a delegate becomes part of its identifying signature

Microsoft Visual C#.NET: From Problem Analysis to Program Design5 Delegates ( continued ) Delegate declaration example delegate string ReturnsSimpleString( ); Delegate signature –Identifies what types of methods the delegate represents Above Example represents methods that return a string and require no argument –static string EndStatement( ) –static string ToString( ) –static string ReturnSaying( )

Microsoft Visual C#.NET: From Problem Analysis to Program Design6 Delegates ( continued ) Associate delegate with method(s) by creating delegate instance(s) –Example ReturnsSimpleString saying3 = new ReturnsSimpleString(EndStatement); Constructor for delegate of the delegate class always takes just parameter – Name of a method for the constructor to reference

Microsoft Visual C#.NET: From Problem Analysis to Program Design7 Delegates ( continued ) Delegate identifier references the method sent as argument to constructor –Any use of delegate identifier now calls the method Methods are said to be wrapped by the delegate –Delegate can wrap more than one method—called a multicast delegate += and -= operators are used to add/remove methods to/ from the delegate chain or invocation list Multicast delegates must have a return type of void

Microsoft Visual C#.NET: From Problem Analysis to Program Design8 Relationship of Delegates to Events Delegates are used for event-driven application –Delegate acts as intermediary between objects that are raising or triggering an event During compilation, the method or methods that will be called are not determined Events as special forms of delegates –Place a reference to event-handler methods inside a delegate –Once reference is made, or event is registered, delegate is used to call event-handler method when an event like a button click is fired

Microsoft Visual C#.NET: From Problem Analysis to Program Design9 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

Microsoft Visual C#.NET: From Problem Analysis to Program Design10 Event Handling in C# ( continued ) Code associates the methods with a delegate this.button1.Click += new System.EventHandler(this.button1_Click); this.button2.Click += new System.EventHandler(this.button2_Click); –System.EventHandler is a delegate type –button1.Click and button2.Click are methods –Keyword this is added to all code generated by Visual Studio.NET to indicate the current instance of a class

Microsoft Visual C#.NET: From Problem Analysis to Program Design11 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 runtime Includes number of properties and events –Items property used to set initial values Click on (Collections) to add items

Microsoft Visual C#.NET: From Problem Analysis to Program Design12 Adding a ListBox Control Object Add ListBox control, then click on Items property (Collection) to type entries

Microsoft Visual C#.NET: From Problem Analysis to Program Design13 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

Microsoft Visual C#.NET: From Problem Analysis to Program Design14 ListBox Control Objects ( continued ) Register its event with the System.EventHandler delegate this.lstBoxEvents.SelectedIndexChanged += new System.EventHandler (this.listBox1_SelectedIndexChanged); Visual Studio.NET adds event handler method private void listBox1_SelectedIndexChanged (object sender, System.EventArgs e) { }

Microsoft Visual C#.NET: From Problem Analysis to Program Design15 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

Microsoft Visual C#.NET: From Problem Analysis to Program Design16 ListBox Control Objects ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design17 Multiple Selections with a ListBox SelectionMode Property has values of MultiSimple, MultiExtended, None and One –MultiSimple: use the spacebar and click of mouse –MultiExtended can also use Ctrl key, Shift key, and arrow keys foreach(string activity in lstBoxEvents.SelectedItems) { result += activity + " "; } this.txtBoxResult.Text = result;

Microsoft Visual C#.NET: From Problem Analysis to Program Design18 ListBox Control Objects ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design19 ListBox Control Objects ( continued ) SelectedItem and SelectedItems returns 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 runtime 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); }

Microsoft Visual C#.NET: From Problem Analysis to Program Design20 ListBoxExample

Microsoft Visual C#.NET: From Problem Analysis to Program Design21 ListBoxExample—Properties Set

Microsoft Visual C#.NET: From Problem Analysis to Program Design22 ListBoxExample—Properties Set ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design23 Properties of the ListBox Class

Microsoft Visual C#.NET: From Problem Analysis to Program Design24 Methods of the ListBox Class Remembe r ListBox object also inherits members from Control class

Microsoft Visual C#.NET: From Problem Analysis to Program Design25 ComboBox Controls Extra TextBox object with ComboBox— User selects from list or types new value

Microsoft Visual C#.NET: From Problem Analysis to Program Design26 ComboBox Controls ( continued ) Top line left blank in ComboBox when DropDownStyle property is set to DropDown (default setting)

Microsoft Visual C#.NET: From Problem Analysis to Program Design27 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

Microsoft Visual C#.NET: From Problem Analysis to Program Design28 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

Microsoft Visual C#.NET: From Problem Analysis to Program Design29 Programming Event Handlers ( continued ) KeyPress( ) event handler method fired with each keystroke

Microsoft Visual C#.NET: From Problem Analysis to Program Design30 Menu Control Objects Drag MainMen u control to form, then click here to display Menu structure

Microsoft Visual C#.NET: From Problem Analysis to Program Design31 Menu Control Objects ( continued ) Preceding the character with an ampersand (&) places an underline under the next character typed –F&ormat—creates Format and Alt+o becomes the shortcut Also have Shortcut property –Can associate any key combination to Menu options

Microsoft Visual C#.NET: From Problem Analysis to Program Design32 Menu Control Objects ( continued ) To create separators, right-click on the text label (below the needed separator) Select Insert Separator

Microsoft Visual C#.NET: From Problem Analysis to Program Design33 Menu Control Objects ( continued ) Set the Menu property on the form to the name of the MainMenu object

Microsoft Visual C#.NET: From Problem Analysis to Program Design34 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

Microsoft Visual C#.NET: From Problem Analysis to Program Design35 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

Microsoft Visual C#.NET: From Problem Analysis to Program Design36 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

Microsoft Visual C#.NET: From Problem Analysis to Program Design37 Adding Predefined Standard Windows Dialog Boxes—Color ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design38 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 ; }

Microsoft Visual C#.NET: From Problem Analysis to Program Design39 GardeningForm Application

Microsoft Visual C#.NET: From Problem Analysis to Program Design40 GardeningForm Application ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design41 GardeningForm Application ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design42 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

Microsoft Visual C#.NET: From Problem Analysis to Program Design43 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

Microsoft Visual C#.NET: From Problem Analysis to Program Design44 CheckBox Object

Microsoft Visual C#.NET: From Problem Analysis to Program Design45 GroupBox Objects ( continued ) 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

Microsoft Visual C#.NET: From Problem Analysis to Program Design46 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

Microsoft Visual C#.NET: From Problem Analysis to Program Design47 RadioButton Objects ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design48 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

Microsoft Visual C#.NET: From Problem Analysis to Program Design49 RadioButton Objects ( continued ) if(this.radBeginner.Checked) { cost +=10; this.lblMsg.Text = "Beginner “ + “-- Extra $10 charge"; } else // more statements

Microsoft Visual C#.NET: From Problem Analysis to Program Design50 ComputeCost_CheckChanged( ) and Click( ) events raised

Microsoft Visual C#.NET: From Problem Analysis to Program Design51 DinerGui Application Example

Microsoft Visual C#.NET: From Problem Analysis to Program Design52 DinerGui Application Example ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design53 DinerGui Application Example ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design54 DinerGui Application Example ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design55 DinerGui Application Example ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design56 DinerGui Application Example ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design57 DinerGui Application Example ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design58 DinerGui Application Example ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design59 Properties—DinerGui Application

Microsoft Visual C#.NET: From Problem Analysis to Program Design60 Properties—DinerGui Application ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design61 DinerGui Application Example ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design62 DinerGui Application Example ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design63 DinerGui Application Example ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design64 DinerGui Application Example ( continued )

Microsoft Visual C#.NET: From Problem Analysis to Program Design65 Chapter Summary Delegates Event-handling procedures –Registering an event ListBox control for multiple selections ComboBox versus ListBox objects Menu controls RadioButton versus CheckBox objects