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.

Slides:



Advertisements
Similar presentations
Introduction to Visual Basic.NET Uploaded By: M.Sheraz anjum.
Advertisements

CHAPTER TWO Creating Simple Visual Basic.NET Windows Applications.
Visual Basic 2010 How to Program. © by Pearson Education, Inc. All Rights Reserved.2.
Visual Basic 2010 How to Program Reference: Instructor: Maysoon Bin Duwais slides Visual Basic 2010 how to program by Deitel © by Pearson Education,
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
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.
1 Pertemuan 02 Visual Basic Environment and Control Matakuliah: D0524 / Algoritma dan Pemrograman Komputer Tahun: 2005 Versi:
Using the Visual Basic Editor Visual Basic for Applications 1.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Copyright © 2012 Pearson Education, Inc. Chapter 2 Introduction to Visual C#
Chapter 13: Object-Oriented Programming
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Programming Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
C# Event Processing Model Solving The Mystery. Agenda Introduction C# Event Processing Macro View Required Components Role of Each Component How To Create.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter One An Introduction to Visual Basic 2010.
Slide 1 Chapter 2 Visual Basic Interface. Slide 2 Chapter 2 Windows GUI  A GUI is a graphical user interface.  The interface is what appears on the.
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
A.k.a. GUI’s.  If you want to discuss your Lab 2 grade come see me this week. ◦ Office: 436 ERB. One hour prior to class ◦ Open to Appointments MWF 
Chapter 8: String Manipulation
A First Program Using C#
Visual Basic Chapter 1 Mr. Wangler.
Hands-on Introduction to Visual Basic.NET Programming Right from the Start with Visual Basic.NET 1/e 6.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Microsoft Visual Basic 2005: Reloaded Second Edition
An Object-Oriented Approach to Programming Logic and Design
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Chapter 8: Writing Graphical User Interfaces
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
CHAPTER TWO Creating Simple Visual Basic.NET Windows Applications.
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 4 I Need a Tour Guide.
Chapter 3: Using GUI Objects and the Visual Studio IDE.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design.
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
Introduction to Visual Basic.NET Chapter 2 Introduction to Controls, Events.
G RAPHICAL U SER I NTERFACE C ONCEPTS : P ART 1 1 Outline Introduction Windows Forms Event-Handling Model - Basic Event Handling.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 - Graphical User Interface Concepts: Part.
CHAPTER TWO Creating Simple Visual Basic.NET Windows Applications.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
1 Chapter Nine Using GUI Objects and the Visual Studio IDE.
Chapter 2 – Introduction to the Visual Studio .NET IDE
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Hands-on Introduction to Visual Basic.NET Programming Right from the Start with Visual Basic.NET 1/e 6.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
Object-Oriented Application Development Using VB.NET 1 Chapter 2 The Visual Studio.NET Development Environment.
Chapter 3 I Need a Tour Guide (Introduction to Visual Basic 2010) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Computing with C# and the .NET Framework
Chapter 1: An Introduction to Visual Basic 2015
Chapter Eleven Handling Events.
How to design a Windows Forms application
Using GUI Objects and the Visual Studio IDE
Using Procedures and Exception Handling
Variables and Arithmetic Operations
C# Event Processing Model
Hands-on Introduction to Visual Basic .NET
CIS16 Application Development Programming with Visual Basic
Chapter 13: Handling Events
Presentation transcript:

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

3 Objectives How to handle Control component events How to add more events to an application How to use the Visual Studio IDE to generate event-handling code How to set Control’s tab order

4 Objectives How to use the sender object in an event How to add a main menu to a Form How to continue your exploration of C#

5 Understanding Delegates A delegate is an object that contains a reference to a method A delegate provides a way to pass a reference to a method as an argument to another method C# provides a compromise between the dangers of C++ pointers and the Java ban on passing functions You declare a delegate using the keyword delegate, followed by an ordinary method declaration including a return type, method name, and argument list

6 Understanding Delegates A delegate can encapsulate any method as long as the method has the same return type and the same number and types of arguments

7 Creating Composed Delegates You can assign one delegate to another using the = operator You can also use the + and += operators to combine delegates into a composed delegate Only delegates with the same argument list can be composed, and the delegates used must have a void return value You can use the – and -= operators to remove a delegate from a composed delegate

8 Creating Composed Delegates Delegate3 Program

9 Creating Composed Delegates Output of Delegate3 program

10 Handling Events In C#, an event occurs when something interesting happens to an object You use an event to notify a client program when something happens to a class object the program is using To declare an event, you use a delegate An event handler requires two arguments—the sender and an EventArgs object

11 Using the Built-in EventHandler The C# language allows you to create events using any delegate type The.NET Framework provides guidelines you should follow if you are developing a class that will be used by others For events that do not use any additional information, the.NET Framework has already defined an appropriate type named EventHandler

12 Handling Control Component Events You can use the same techniques to handle events generated or raised by GUI Controls as you do to handle events raised by non-Control-generated events

13 Handling Control Component Events Some Control Class Public Instance Events (continued)

14 Adding More Events to an Application A Form can contain any number of Controls that might have events associated with them A single control might be able to raise any number of events

15 Using the IDE to Generate Event-Handling Code It is possible (and easier) to create event-handling code using the IDE The only difference between event-handling code created by hand and the one you create using the IDE is that the IDE automatically creates a lot of code for you The advantage of using the IDE is the time you save typing and correcting typing errors The advantage of creating methods by hand is that you understand what each statement accomplishes and are able to customize methods to perform exactly the task you want

16 Setting Controls’ Tab Order A Control is said to have focus if it raises an event when the user presses Enter TabStop is a Boolean property of a Control that identifies whether the Control will serve as a stopping place in a sequence of Tab key presses TabIndex is a numeric property that indicates the order in which the Control will receive focus when the user presses the Tab key When a Control has a TabIndex of 0, it receives focus when the Form is initialized

17 Setting Controls’ Tab Order ManyButtons Form with three Buttons

18 Using the sender Object in an Event When a Form contains multiple widgets that you can manipulate, you can write event handling methods for each one When a Form contains multiple widgets, you can also write a single event-handling method that can take appropriate action based on the Control that generated the event The Control that causes an event is represented as a generic object in the object sender argument to an event method

19 Using the sender Object in an Event ManyButtons Form including Label

20 Using the sender Object in an Event Every object has an Equals() method that can be used to determine the sender object

21 Adding a Main Menu to a Form Most programs you use in a Windows environment contain a main menu You can add a main menu to a Form by using the MainMenu Control

22 Continuing to Explore C# The Visual Studio IDE contains numerous Controls, each containing hundreds of properties and events There are many resources that are available to use to learn more about C#, including: –Help facility in the Visual Studio IDE –Tutorials in the Help facility –C# discussion groups on the Internet

23 Chapter Summary A delegate is an object that contains a reference to, or encapsulates, a method You can assign one delegate to another using the = operator. You also can use the + and ++ operators to combine delegates into a composed delegate that calls the delegates from which it is built. In C#, an event occurs when something “interesting” happens to an object For events that do not use any additional information, the.NET Framework has defined an appropriate delegate type named EventHandler

24 Chapter Summary When you use Controls like Buttons and ListBoxes, they already contain events with names like Click, DragOver, MouseEnter, and MouseLeave A Form can contain any number of Controls that might have events associated with them When designing a Form with events, you can use the Visual Studio IDE to automatically create a lot of code for you When users encounter multiple GUI Controls on a Form, usually one Control has Focus

25 Chapter Summary When a Form contains multiple widgets that you can manipulate, you can write event-handling methods for each one Most programs you use in the Windows environment contain a main menu, which is a horizontal list of general options that appears under the title bar of a Form If you understand good programming principles and, more specifically, the syntax and structure of C# programs, you will find learning about each new C# feature easier than learning about the last one