V 1.1 Programming III. Creating additional windows Event handling: preview/routed events.

Slides:



Advertisements
Similar presentations
Module 1: Creating an Application by Using Windows Presentation Foundation Overview of WPF Creating a Simple WPF Application Handling Events and Commands.
Advertisements

 2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User Interface Concepts: Part 1.
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.
Chapter 1: An Introduction to Visual Basic 2012
Building Applications Dialogs Passing data between windows Validating Input using FocusListeners.
CST238 Week 3 Questions / Concerns? Recap Check-off Take Home Lab#2 New topics – PictureBox – FlowLayoutPanel – Menu – Context Menu – Status Bar – Toolstrip.
Microsoft Word 2010 Lesson 1: Introduction to Word.
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.
CS0004: Introduction to Programming Events. Review  Event Procedure  A set of instructions to be executed when a certain event happens.  Many event-driven.
Chapter 3 Introduction to Event Handling and Windows Forms Applications.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Tutorial: Introduction to ASP.NET Internet Technologies and Web Application 4 th February 2010.
Neal Stublen Tonight’s Agenda  More form controls  Multi-form projects  Application debugging  Object-oriented programming and.
BIM313 – Advanced Programming Simple Controls 1. Contents Traditional Controls – Labels, Text Boxes, Buttons, Check Boxes, List Boxes, Combo Boxes Advanced.
Hello World In C++ and Microsoft Visual C++. Directions to begin a project 1. Go to All Programs 2. Open Visual Studio C++ 3. Click on New Project 4.
V 1.0 Programming III. Creation of additional windows Routed events.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
V 1.1 Programming III. GUI APIs WPF Hello World Important UI elements UI elements / content models UI elements / inheritance.
An Introduction to Silverlight Matt Harrington Developer Evangelist, Microsoft October 20, 2011.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
V 1.1 Programming III. Important events, properties, methods of UI elements XAML basics.
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.
V 1.0 Programming III. Comparison of GUI APIs WPF Hello World UI elements Content models Inheritance chains.
Windows Presentation Foundation Adam Calderon Principal Engineer Interknowlogy LLC
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
CST238 Week 4 Questions / Concerns? Announcements – Start thinking about project ideas – Test#1 next Monday, 4/28/14 Recap Check-off Take Home Lab#3 New.
V 1.0 Programming III. Automatic notifications (…Changed, INofityPropertyChanged, ObservableCollection ) Data formatters Data conversions Resources.
Graphical User Interface Concepts - Part 2 Session 09 Mata kuliah: M0874 – Programming II Tahun: 2010.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
LAYOUT CONTROLS. XAML Overview XAML : eXtensible Application Markup Language pronounced (ZAMEL) is a markup language used to design user interfaces XML-based.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 3 Welcome Application Introduction to Visual Programming.
BIL528 – Bilgisayar Programlama II Introduction 1.
Sample Application Multi Layered Architecture (n-tier): –Graphical User Interface (GUI): Forms, components, controls The Visual Designer in Visual Studio.
Chapter Two Creating a First Project in Visual Basic.
BIM211 – Visual Programming Interacting with Users Graphics 1.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
C# GUI - Basics. Objectives.NET supports two types: WinForms, traditional, desktop GUI apps. WebForms – newer, for Web apps. Visual Studio.NET supports.
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.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Windows Presentation Foundation (WPF). Introduction Separates appearance of user interface from behavior Appearance usually specified by XAML Behavior.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e. © by Pearson Education, Inc. All Rights Reserved.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
GUI development with Matlab: GUI Front Panel Components GUI development with Matlab: Other GUI Components 1 Other GUI components In this section, we will.
Module 4 Taking Control of the User Interface. Module Overview Sharing Logical Resources in an Application Creating Consistent User Interfaces by Using.
Architecture Multi Layered Architecture (n-tier): Application: Model Controllers Database Access Graphical User Interface (GUI): Forms, components, controls.
V 1.0 Programming III. Automatic notifications with data binding (…Changed, INofityPropertyChanged, ObservableCollection, DataTemplate) Data formatters.
CPSC 481 – Week #7 Sowmya Somanath
Understanding JavaScript and Coding Essentials Lesson 8.
 2002 Prentice Hall. All rights reserved. 1 Outline Mouse Event Handling Keyboard Event Handling Graphical User Interface Concepts:
Graphical User Interface Components Version 1.1. Obectives Students should understand how to use these basic Form components to create a Graphical User.
V 1.0 Programming III. XAML Data Binding I.. V 1.0ÓE-NIK, 2014 XAML namespaces Namespaces define the allowed tags and attributes The one without the prefix.
1 Windows Forms II Chapter RadioButton / GroupBox Controls Used to solicit a multiple choice input. Radio buttons work as a group. Selecting one.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 11 Data Files.
V 1.0 Programming III. Visual/FrameworkElement descendants.
Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Presentation Foundation Ruwan Wijesinghe.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
WEB SYSTEMS & TECHNOLOGY. Java Script  JavaScript created by Netscape  It is also client side programming  It can be use for client side checks.
Part of the Microsoft.NET Framework 3.0 Tomer Shamam.NET Technologies Expert Sela Group
GUI Programming Prepared by: Ahmad Ramin Rahimee Assistant Professor ICTI.
Dialog Boxes Getting information from user. Types of Windows Five types we’ll be using 1. Main Application Window 2. Child Windows 3. Dialog Boxes 4.
Important New Concepts In WPF
Validating User Input Lesson 5.
Predefined Dialog Boxes
Multi-form applications and dialogs
Part A – Doing Your Own Input Validation with Simple VB Tools
1/10/2019 JavaFX Events COSC 330.
Web Development Using ASP .NET
6. WinForms 2003 C# GUI - Basics.
Brown Bag Seminar Summer 2007
Presentation transcript:

V 1.1 Programming III. Creating additional windows Event handling: preview/routed events

V 1.1ÓE-NIK, Creating additional windows Two ways to display a new window: –Modal windows: “always on top”, the user cannot switch to another window (warning/error/confirmation or data input) –Non-modal windows: other windows can be used meanwhile the new window is open (special case: MDI) Possibilities: –MessageBox class (only modal) –Microsoft.Win32 dialog windows (only modal) –Inheriting from the Window class (modal or non-modal) –Some old Windows Forms dialog windows are not in WPF: FolderBrowserDialog (WHY???), ColorDialog, FontDialog, PageSetupDialog, PrintPreviewDialog (not so big problem + conflicting types)

V 1.1ÓE-NIK, MessageBox class Display pre-defined simple dialog windows: message, buttons, icons Static method: MessageBox.Show –The parameters determine the message, the header, the buttons and the icons to be displayed –Return value: MessageBoxResult enum value Usage: MessageBox.Show(“Single line message"); MessageBox.Show(“Multi line\nmessage\nwith title", “TITLE");

V 1.1ÓE-NIK, MessageBox parameters I. Buttons: MessageBoxButton enum: The return values are the same enum values, only in the MessageBoxResult enum type: –MessageBoxResult.OK, MessageBoxResult.Cancel, MessageBoxResult.Yes… –Closing the window without answering is usually MessageBoxResult.Cancel, except if only the OK button is shown –WPF has 4 different button sets, Windows Forms had 6... Enum valueDescription OKOK button OKCancelOK + Cancel buttons YesNoYes + No buttons (no way to close it without answering) YesNoCancelYes + No + Cancel buttons MessageBox.Show(“Multi line\nmessage\nwith title", “TITLE“, MessageBoxButton.YesNoCancel);

V 1.1ÓE-NIK, MessageBox parameters II. Icon: MessageBoxImage enum: MessageBox.Show(“Multi line\nmessage\nwith title", “TITLE“, MessageBoxButton.YesNoCancel, MessageBoxImage.Error);

V 1.1ÓE-NIK, 2015 Microsoft.Win32 dialog windows OpenFileDialog, SaveFileDialog A ShowDialog() return value: –bool? type (nullable bool) –true: the user successfully completed the interaction (the values can be used) –false: „cancel” type exit (the values cannot be used) –null: the window is still open DIFFERENT than in Windows Forms! We must ALWAYS check for this value! 6 OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() == true) { string fileName = openFileDialog.FileName; //... Open file, read file... }

V 1.1ÓE-NIK, 2015 Homework – mini notepad 7

V 1.1ÓE-NIK, 2015 Homework – extend it! 8

V 1.1ÓE-NIK, Inheriting from the Window class Creating our own custom windows: 1..xaml +.xaml.cs files are required: Project -> Add -> Window... Same usage as our first window: use the designer, edit properties, add event handlers... 2.Create an instance, then use Show() or ShowDialog() Show? –window.Show();  non modal, non-blocking method call –window.ShowDialog();  modal, with a bool? return value; this is a BLOCKING method call! MyWindow window = new MyWindow(); window.Show(); MyWindow window = new MyWindow(); if (window.ShowDialog() == true) { //... }

V 1.1ÓE-NIK, 2015 Inheriting from the Window class To give information after a ShowDialog() call, we have to set the this.DialogResult property within the new window –This will also instantly close the new window Button controls have the following related properties: –IsCancel – This button will be “clicked” when pressing the ESC key, this will also close the window –IsDefault – This button will be “clicked” when pressing the ENTER/RETURN key Will not close the window Usually we set the DialogResult in the event handler 10 private void Button_Click(object sender, RoutedEventArgs e) { this.DialogResult = true; }

V 1.1ÓE-NIK, Inheriting from the Window class To get data from a modal window, we use properties –Create one property for every data field –Don’t access controls directly! –x:Name="myTextBox" x:FieldModifier="private" In the activator window, we first check the dialog result and then we use the property values string name; string birthPlace; int age; PersonalDataWindow dataInput = new PersonalDataWindow(); if (dataInput.ShowDialog() == true) { name = dataInput.Name; birthPlace = dataInput.BirthPlace; age = dataInput.Age; }

V 1.1ÓE-NIK, 2015 Window properties, methods, events The Window class is a ContentControl descendant –We can use all properties/events of the base class (Width, Height, Foreground, Background… MouseDown, MouseUp, KeyDown, KeyUp, PreviewXXX… Loaded…) Main properties: –Title –Topmost –WindowStartupLocation –WindowState (maximized, minimized, normal) 12

V 1.1ÓE-NIK, 2015 Window properties, methods, events Important methods: –Show() – modal –ShowDialog() – non-modal –Close() –Activate() – bring to front –Hide() Important events –Closed –Closing – before being closed –Activated –Deactivated 13

V 1.1ÓE-NIK, 2015 Homework – list of workers, sick/on holiday 14

V 1.1ÓE-NIK, 2015 Event handling: preview/routed events Main problems: –We know there can be several UI elements in a window –We would like to know when the user moves the mouse ANYwhere! „Traditionally” we should handle the MouseMove event of all controls… –Also, there can be many UI elements within a single ContentControl –E.g.: Button, with a StackPanel and two Rectangle controls = pause button –What did the user clicked on? We don’t really care which part of the visible/non visible controls  we want to handle the single Click event Solution: an event is not only fired on one single control, but also on the container controls as well! 15

V 1.1ÓE-NIK, 2015 Routed Events Most of the UI events are routed events –The event is not only fired on the control where it actually occurred –It will (or: it can) be fired on the parent controls as well  it is possible to handle controls only on the parent  homework: do the calculator with *ONE* event handler! The UI elements are organized into a tree: element tree –ContentControl, ItemsControl elemeknél: Content, Items –Layout managers: Children –Some UI elements can be more complex inside: Button + string content = Button + Border + ContentPresenter + TextBlock) We usually talk about two trees: logical tree, visual tree 16

V 1.1ÓE-NIK, 2015 Trees of UI elements The logical tree shows the connections between the different UI elements: The visual tree also contains the inner structure of all UI elements – this is a lot bigger construction! 17

V 1.1ÓE-NIK, 2015 Trees of UI elements 18 MainWindow Border AdornerDecorator AdornerLayerContentPresenter Grid LabelButton Border ContentPresenter TextBlock Border ContentPresenter TextBlock Logical tree –In red –Shown in the Document Outline Visual tree –Red + Cyan –Can be shown while debugging

V 1.1ÓE-NIK, 2015 Routed events The events are “forwarded” on the tree nodes Logical tree: dynamic resources, the element name lookups during the data binding Visual tree: render events, transparency, transformations, IsEnabled, hit test Hybrid: dependency properties, forwarded events Forwarding strategies: –Upwards („bubbling”): first on the UI element that had the event, then goes up to the parent, until the root –Downwards („tunneling”): first on the root, then through the children towards the UI element that had the event –„Direct”: not forwarded, the event is fired only on the UI element that had the event The documentation defines the forwarding strategy for all events in the framework 19

V 1.1ÓE-NIK, 2015 Preview events Some events, e.g.: –KeyDown –KeyUp –MouseDown –MouseUp –… … have a Preview… pair: –PreviewKeyDown –PreviewKeyUp –PreviewMouseDown –PreviewMouseUp 20

V 1.1ÓE-NIK, 2015 Preview/routed events A PreviewXXX-XXX pairs: –The PreviewXXX uses the „tunneling” strategy –The XXX uses the „bubbling” strategy –The PreviewXXX will always fire first PreviewMouseDown, MouseDown example 1.PreviewMouseDown on the MainWindow 2.PreviewMouseDown on the Grid 3.PreviewMouseDown on the Label 4.MouseDown on the Label 5.MouseDown on the Grid 6.MouseDown on the MainWindow 21 MainWindow Grid LabelButton

V 1.1ÓE-NIK, 2015 Preview events The preview event is always fired before the “real” event Usage: –Pre-processing –Block the „real” event (e.Handled) 22 private void textBoxText_PreviewKeyDown(object sender, KeyEventArgs e) { e.Handled = true; //result: we can’t type to the textbox }

V 1.1ÓE-NIK, 2015 Small variations 23 Some exceptions exist to the usual processing in the Preview→”real” event forwarding: –E.g. the Button events –The “purpose” of the Button is to create a Click event –This converts the PreviewMouseDown and …Up-ot into a Click event and blocks some of the further events –The MouseDown will not be fired after the PreviewMouseDown Some exceptions exist to the usual event handling –The MouseDown event of the Grid is always affected when clicking on a control inside the Grid –But if it is clicked directly, then the MouseDown is only fired if the Grid is visible (its Background is not null) –Otherwise, only the MouseDown event of the MainWindow is fired!

V 1.1ÓE-NIK, 2015 Routed events To know where the event comes from… –sender: the UI element that is executing the current event handler. NOT NECESSARILY the element that had the event! –e.Source: The element that had the event (logical tree) –e.OriginalSource: The element that had the event (visual tree) 24

V 1.1ÓE-NIK, 2015 Exercise – Simplified Android password input Image source: enable-pattern-lock-security-on-android-devices/ Extra task: when entering the good password, display another window with the current applications + a textbox to change the password 25