Events in C# MHA - 20141. Delegates vs. Events Delegates can be used as events Example CountDownTimerEvent -> CountDownDelegate But have certain problems.

Slides:



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

CS7026 jQuery Events. What are Events?  jQuery is tailor-made to respond to events in an HTML page.  Events are actions that can be detected by your.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
Threads in C# Threads in C#.
Julian on JavaScript: Using jQuery with DevExpress ASP.NET controls Julian M Bucknall, CTO Mehul Harry, ASP.NET Tech Evangelist.
Mouse event handling Mouse events( delegates EventHandler, event arguments EventArgs) MouseEnter: raised if the mouse cursor enters the area of the control.
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.
Events. Events Single Event Handlers Click Event Mouse Events Key Board Events Create and handle controls in runtime Outline.
XP Tutorial 8 New Perspectives on JavaScript, Comprehensive1 Working with the Event Model Creating a Drag-and-Drop Shopping Cart.
C# Event Processing Model Solving The Mystery. Agenda Introduction C# Event Processing Macro View Required Components Role of Each Component How To Create.
1 Windows Graphics. 2 Objectives You will be able to Use the Windows GDI+ to draw arbitrary figures and text on a Windows form. Add a handler for the.
Events in C# Events in C#.
Delegates and lambda functions Jim Warren, COMPSCI 280 S Enterprise Software Development.
CSC 298 Windows Forms.
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.
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)
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Controls. Adding Controls to Form -You can pick controls from the toolbox. -To add the controls from Toolbox to the Form You have be in design view. -To.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Object Oriented Programming.  Interface  Event Handling.
Inside LINQ to Objects How LINQ to Objects work Inside LINQ1.
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.
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:
Appendix A: Windows Forms. 2 Overview Describe the structure of a Windows Forms application –application entry point –forms –components and controls Introduce.
CIS 338: Events Dr. Ralph D. Westfall April, 2011.
Arrays & Enum & Events. Arrays Arrays are data structures consisting of related data items of the same type. Arrays are fixed-length entities—they remain.
Implement User Input Windows Development Fundamentals LESSON 2.4A.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College Lecture 4: Intro to GUIs and the.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
CIS NET Applications1 Chapter 6 – Events. CIS NET Applications2 Objectives Delegate-based Events.NET event support Practical guides for managing.
Module 5: Programming with C#. Overview Using Arrays Using Collections Using Interfaces Using Exception Handling Using Delegates and Events.
Events and Event Handling
Delegates and Events Svetlin Nakov Telerik Corporation
Computing with C# and the .NET Framework
Event Loops and GUI Intro2CS – weeks
Critical sections, locking, monitors, etc.
Chapter Eleven Handling Events.
Chapter 5: Programming with C#
Delegates/ Anders Børjesson
Windows Desktop Applications
Lua Scripting in C#.
Event Driven Programming
C# Event Processing Model
Delegates & Events 1.
Generics in C# / Anders Børjesson
Visual programming Chapter 2: Events and Event Handling
A few tricks to take you beyond the basics of Microsoft Office
5.4 Deleting a Module To delete a specific module of a page, click on the Delete inside the Content module menu. Use this facility with outmost care since.
Exception Handling and Event Handling
Programming in C# CHAPTER - 8
Chapter 13: Handling Events
Events, Delegates, and Lambdas
Presentation transcript:

Events in C# MHA

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#2

Event syntax New keyword: event General syntax Public event SomeDelegateType SomeName; Examples Public event Action Finished; Public event Action 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#3

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#4

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#5

Windows/Web 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/Web 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#6

References and further reading MSDN Events Tutorial Bart De Smet: C# 5.0 Unleashed, Sams 2013 Chapter 18: Events, page Nagel et al. Professional C# 5.0 and.NET 4.5.1, Wrox 2014 Chapter 8: Delegates, Lambdas, and Events, page Og vi tager lige et hurtig kig på: SimplestEventExample Events in C#7