Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Based on Events

Similar presentations


Presentation on theme: "Programming Based on Events"— Presentation transcript:

1 Programming Based on Events
10 C# Programming: From Problem Analysis to Program Design 3rd Edition C# Programming: 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 C# Programming: From Problem Analysis to Program Design

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 Design and create a Windows Presentation Foundation (WPF) application Work through a programming example that illustrates the chapter’s concepts C# Programming: From Problem Analysis to Program Design

4 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 C# Programming: From Problem Analysis to Program Design

5 Defining Delegates Delegate declaration example Delegate signature
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( ) C# Programming: From Problem Analysis to Program Design

6 Creating Delegate Instances
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 C# Programming: From Problem Analysis to Program Design

7 Using Delegates 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 C# Programming: From Problem Analysis to Program Design

8 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 C# Programming: From Problem Analysis to Program Design

9 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 C# Programming: From Problem Analysis to Program Design

10 Event-Handler Methods
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 to indicate the current instance of a class C# Programming: From Problem Analysis to Program Design

11 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 is used to set initial values Click on (Collections) to add items C# Programming: From Problem Analysis to Program Design

12 Adding a ListBox Control Object
Add ListBox control, then click on Items property (Collection) to type entries Figure 10-2 String Collection Editor C# Programming: From Problem Analysis to Program Design

13 Registering a ListBox Event
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 C# Programming: From Problem Analysis to Program Design

14 ListBox Event Handlers
Register 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

15 ListBox Control Objects
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 C# Programming: From Problem Analysis to Program Design

16 ListBox Control Objects (continued)
Figure 10-3 SelectedIndexChanged event fired C# Programming: From Problem Analysis to Program Design

17 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; C# Programming: From Problem Analysis to Program Design

18 ListBox Control Objects (continued)
Figure 10-4 Multiple selections within a ListBox object C# Programming: From Problem Analysis to Program Design

19 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 C# Programming: From Problem Analysis to Program Design

20 Adding Items to a ListBox
Add items at run time 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); } C# Programming: From Problem Analysis to Program Design

21 ListBoxExample Figure 10-5 Add( ) method executed inside the buttonClick event C# Programming: From Problem Analysis to Program Design

22 C# Programming: From Problem Analysis to Program Design

23 ListBox Control Properties
C# Programming: From Problem Analysis to Program Design

24 C# Programming: From Problem Analysis to Program Design

25 ComboBox Objects In many cases, ListBox controls and ComboBox controls can be used interchangeably ListBox control is usually used when you have all the choices available at design time ComboBox facilitates displaying a list of suggested choices with an added feature ComboBox objects contain their own text box field as part of the object C# Programming: From Problem Analysis to Program Design

26 Adding ComboBox Controls
Extra TextBox object with ComboBox – User selects from list or types new value Figure 10-6 ComboBox and ListBox objects C# Programming: From Problem Analysis to Program Design

27 Adding ComboBox Controls (continued)
Top line left blank in ComboBox when DropDownStyle property is set to DropDown (default setting) Figure 10-7 ComboBox list of choices C# Programming: From Problem Analysis to Program Design

28 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 C# Programming: From Problem Analysis to Program Design

29 Programming Event Handlers
Since ListBox object allows multiple selections, Text property cannot be used Text ONLY gets the first one selected Use 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 C# Programming: From Problem Analysis to Program Design

30 Programming Event Handlers (continued)
KeyPress( ) event-handler method fired with each keystroke Figure 10-8 KeyPress and SelectedIndexChanged events fired C# Programming: From Problem Analysis to Program Design


Download ppt "Programming Based on Events"

Similar presentations


Ads by Google