Events in C# Events in C#.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Advertisements

Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
Threads in C# Threads in C#.
Mouse event handling Mouse events( delegates EventHandler, event arguments EventArgs) MouseEnter: raised if the mouse cursor enters the area of the control.
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.
C# Event Processing Model Solving The Mystery. Agenda Introduction C# Event Processing Macro View Required Components Role of Each Component How To Create.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
Events in C# MHA Delegates vs. Events Delegates can be used as events Example CountDownTimerEvent -> CountDownDelegate But have certain problems.
CSC 298 Windows Forms.
C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,
Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design.
Dr Dat Tran - Week 4 Lecture Notes 1 ToolStrip Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences &
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
References types and Value types With a very brief introduction to struct and enum Reference types and Value types1.
G RAPHICAL U SER I NTERFACE C ONCEPTS : P ART 1 1 Outline Introduction Windows Forms Event-Handling Model - Basic Event Handling.
Carnegie Mellon University MSCF1 C#/.NET Basics 2 Some code is from “C# in a Nutshell”
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 - Graphical User Interface Concepts: Part.
Visual C# 2012 How to Program 1. 2  A graphical user interface (GUI) allows a user to interact visually with a program.  Figure 14.1 shows a Visual.
Interfaces 1. Interfaces are (parts of) contracts Interfaces are contracts between implementers and consumers Consumers: Programmers using a class implementing.
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.
– Advanced Programming P ROGRAMMING IN Lecture 21 Introduction to Swing.
Delegates and Events Callback Functionality and Event-Driven Programming Svetlin Nakov Technical Trainer Software University
Programming with Visual C++: Concepts and Projects Chapter 2B: Reading, Processing and Displaying Data (Tutorial)
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Observer design pattern A closer look at INotifyPropertyChanged, INotifyPropertyChanging and ObservableCollection Observer design pattern1.
Object Oriented Programming.  Interface  Event Handling.
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 27 JavaBeans and.
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
(1) Introduction to Java GUIs Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
Generics in C# 1. Generics List vs. non-generic ArrayList Generic List Namespace System.Collections.Generic List list = new List (); List.add(”Anders”);
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
Designing user interfaces using: Simple views 1. Views Basic views – TextView – EditText – Button – ImageButton – CheckBox – ToggleButton – RadioButton.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 32 JavaBeans and Bean.
Simple Clicker App WPF App using C#. App Requirement Need a ‘counter’ display, which starts at 0 Need a ‘clicker’ button ! Pressing the clicker every.
Microsoft Code Contracts How to program Pre-conditions, Post-conditions, and Object Invariants Microsoft Code Contracts1.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 15 Event-Driven Programming and.
 2002 Prentice Hall. All rights reserved. 1 Outline Mouse Event Handling Keyboard Event Handling Graphical User Interface Concepts:
Events Event Handling in JavaScript SoftUni Team Technical Trainers Software University
CIS 338: Events Dr. Ralph D. Westfall April, 2011.
Implement User Input Windows Development Fundamentals LESSON 2.4A.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
Events and Event Handling
Patterns in programming
INF230 Basics in C# Programming
Delegates and Events Svetlin Nakov Telerik Corporation
Computing with C# and the .NET Framework
Event Loops and GUI Intro2CS – weeks
Important New Concepts In WPF
Critical sections, locking, monitors, etc.
Chapter Eleven Handling Events.
Chapter 5: Programming with C#
Delegates/ Anders Børjesson
Windows Desktop Applications
Event Driven Programming
C# Event Processing Model
Gathering User Input Event Handling Create Dynamic controls
Delegates & Events 1.
Visual programming Chapter 2: Events and Event Handling
Programming in C# CHAPTER - 8
Events, Event Handlers, and Threads
Chapter 13: Handling Events
Chengyu Sun California State University, Los Angeles
Events, Delegates, and Lambdas
05 | Capturing User Input Michael “Mickey” MacDonald | Indie Game Developer Bryan Griffiths | Software Engineer/Game Developer.
Brown Bag Seminar Summer 2007
Presentation transcript:

Events in C# Events in C#

Delegates vs. Events Delegates can be used as events Example CountDownTimerEvent -> CountDownDelegate But have certain problems Delegate can be “hijacked” Outsiders can make additional events The Event type overcomes these problems Example CountDownTimerEvent -> CountDownEvent Events in C#

Event syntax New keyword: event General syntax Public event SomeDelegateType SomeName; Examples Public event Action Finished; Public event Action<uint> Tick; Adding (attaching) and removing (detaching) event handlers Event handlers are method implementing the Delegate Type Add using the += operator Remove using the -= operator Example: CountDownTimerEvent Events in C#

Raising events: Beware of null! Unassigned events are null! Careful when you raise an event If (Tick != null) Tick(3); Is dangerous if another thread detaches a handler right after the if condition is evaluated Example: CountDownTimerEvent Why is the event null, and not just an empty List or something? Some classes has a lot of events, of which most are hardly ever used And we do not want to have a lot of empty List objects in memory Events in C#

Some classes have lots of different events A Windows Forms Button has 68(!) events Some examples BackgroundColorChanged BackgroungImageChanged Click DoubleClick DragDrop FontChanged GotFocus KeyDown MouseHover Resize And a lot more … Most of these events you would never ever use Events in C#

Many-to-many relationship Multicast events One event can raise multiple event handlers Multi-handle One event handler can handle events from multiple sources Events in C#

Windows Forms: Event Handlers and EventArgs The delegate type Action can be used as the basis for GUI events, but it is not use as it is … Windows Forms event handlers EventHandler, for button clicks Delegate void EventHandler(Object sender, EventArgs e) KeyEventHandler, for keyboard Delegate void KeyEventHandler(Object sender, KeyEventArgs e) MouseEventHandler, for mouse events Delegate void MouseEventHandler(Object sender, MouseEventHandler e) Etc. Events in C#

WPF: Event handlers and RoutedEventArgs WPF = Windows Presentation Foundation GUI API as Windows Forms, but newer Example: concurrency -> SimpleBrowserAsync RoutedEventArgs : EventArgs RoutedEventArgs extends EventArgs Example: CountDownTimerEvent -> CountDownWpfApp Events in C#

References and further reading MSDN Events Tutorial http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx Bart De Smet: C# 5.0 Unleashed, Sams 2013 Chapter 18: Events, page 843-911 Nagel et al. Professional C# 5.0 and .NET 4.5.1, Wrox 2014 Chapter 8: Delegates, Lambdas, and Events, page 183-208 Events in C#