Gathering User Input Event Handling Create Dynamic controls

Slides:



Advertisements
Similar presentations
 2007 Dr. Natheer Khasawneh. Chapter 13. Graphical User Interface Concepts: Part 1.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 11 MORE WINDOWS CONTROLS & STANDARD DIALOG BOXES.
C# - Files and Streams Outline Files and Streams Classes File and Directory Creating a Sequential-Access File Reading Data from a Sequential-Access.
Chapter 11 Data Files Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
A graphical user interface (GUI) is a pictorial interface to a program. A good GUI can make programs easier to use by providing them with a consistent.
Programming Based on Events
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 2005 CHAPTER 8 Using Procedures and Exception Handling.
Visual Basic 2008 Express Edition The IDE. Visual Basic 2008 Express The Start Page Recent Projects Open an existing project Create a New Project.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
V 1.0 Programming III. Creation of additional windows Routed events.
Introduction to Graphical User Interfaces. Objectives * Students should understand what a procedural program is. * Students should understand what an.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
Adding User Interactivity – Lesson 51 Adding User Interactivity Lesson 5.
C# Events and WPF #W5. Horizontal Prototype WPF Designed for rapid user interface design Used for many devices: Windows Phone, Tablets, PCs,
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Dr Dat Tran - Week 4 Lecture Notes 1 ToolStrip Programming Graphical User Interfaces PG (7110) University of Canberra School of Information Sciences &
File I/O 11_file_processing.ppt
G RAPHICAL U SER I NTERFACE C ONCEPTS : P ART 1 1 Outline Introduction Windows Forms Event-Handling Model - Basic Event Handling.
Lecture 2 COM - Example. Integrate the web Webcam functionality using.Net and COM First let's explain what we should do exactly. First let's explain what.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
BIL528 – Bilgisayar Programlama II Introduction 1.
Introduction to Windows Programming
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
Concurrent Programming and Threads Threads Blocking a User Interface.
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.
CS-1020 Dr. Mark L. Hornick 1 Event-Driven Programming.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Object Oriented Programming.  Interface  Event Handling.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
Select (drop-down list) Inputs. Insert/Form/List Menu.
Module 8 Enhancing User Interface Responsiveness.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Events Programming with Alice and Java First Edition by John Lewis.
6-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
CPSC 481 – Week #7 Sowmya Somanath
Module 1 Introducing C# and the.NET Framework. Module Overview Introduction to the.NET Framework 4 Creating Projects Within Visual Studio 2010 Writing.
User Interface Programming in C#: Basics and Events Chris North CS 3724: HCI.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 11 Data Files.
MATLAB and SimulinkLecture 61 To days Outline Graphical User Interface (GUI) Exercise on this days topics.
JavaScript Events Java 4 Understanding Events Events add interactivity between the web page and the user You can think of an event as a trigger that.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
JavaScript Events. Understanding Events Events add interactivity between the web page and the user Events add interactivity between the web page and the.
CIS NET Applications1 Chapter 8 – Multithreading and Concurrency Management.
REC [ ] How to implement CAMERA RECORDING for USB WEBCAM or IP CAMERA in C#.NET SOURCE CODE: ! Welcome to this presentation that explains.
CompSci 230 S Programming Techniques
Chapter 9 Programming Based on Events
INF230 Basics in C# Programming
Computing with C# and the .NET Framework
Allowing File Uploads.
Introduction to Event-Driven Programming
Chapter Topics 15.1 Graphical User Interfaces
Chapter Eleven Handling Events.
Using Procedures and Exception Handling
Windows Desktop Applications
Event Driven Programming
Sequential Input and Output using Text Files
Predefined Dialog Boxes
C# Event Processing Model
Event Driven Systems and Modeling
Event loops 17-Jan-19.
Chapter 15: GUI Applications & Event-Driven Programming
Constructors, GUI’s(Using Swing) and ActionListner
Chapter 5 Processing Input with Applets
Overview of the IDE Visual Studio .NET is Microsoft’s Integrated Development Environment (IDE) for creating, running and debugging programs (also.
Allowing File Uploads.
UI Elements 2.
Presentation transcript:

Gathering User Input Event Handling Create Dynamic controls Using SaveFileDialog Class Improving Responsiveness in a WPF Apps

Event Handling When a user interacts with a GUI control (e.g., clicking a button on a form), one or more methods are executed in response to the above event.  Events can also be generated without user interactions. Event handlers are methods in an object that are executed in response to some events occurring in the application private void FareTikla() {      // Buraya mouse’un sol tıklanması durumunda yapılması gereken      // işlemler gelecek. } Mouse.MouseClicked += new MouseClickedEventHandler(FareTikla);

 You will observe that you do not have to declare the delegates and reference those delegates using event keyword because the events (mouse click, etc.) for the GUI controls (Form, Button, etc.) are already available to you and the delegate is System.EventHandler. XAML <MenuItem Header="_New Member" Name="newMember" Click=> C# private void newMember_Click(object sender, RoutedEventArgs e) { }

Create Dynamic controls public MainWindow() { InitializeComponent(); this.Reset(); MenuItem saveMemberMenuItem = new MenuItem(); saveMemberMenuItem.Header = "Save Member Details"; saveMemberMenuItem.Click += new RoutedEventHandler(saveMember_Click); windowContextMenu = new ContextMenu(); windowContextMenu.Items.Add(saveMemberMenuItem); windowContextMenu.Items.Add(clearFormMenuItem); } private void newMember_Click(object sender, RoutedEventArgs e) ... this.ContextMenu = windowContextMenu;

Using SaveFileDialog Class The SaveFileDialog component allows users to browse the file system and select files to be saved. The dialog box returns the path and name of the file the user has selected in the dialog box. However, you must write the code to actually write the files to disk. private void saveMember_Click(object sender, RoutedEventArgs e) { SaveFileDialog saveDialog = new SaveFileDialog(); saveDialog.DefaultExt = "txt"; saveDialog.FileName = "Members"; saveDialog.InitialDirectory = @"C:\Users\YourName\Documents\"; saveDialog.Title = "Bell Ringers"; ... }

Simulate a long-running event handler in a WPF application Improving Responsiveness in a WPF Apps You have seen that your application responds to the user performing operations such as clicking buttons, typing text into boxes, or selecting menu items by using code that runs when the corresponding events are triggered. However, what happens if the code that responds to an event takes a long time to run? Simulate a long-running event handler in a WPF application private void saveMember_Click(object sender, RoutedEventArgs e) { ….. Thread.Sleep(10000); MessageBox,Show("Member details saved", "Saved"); } The static Sleep method of the Thread class in the System.Threading namespace causes the current thread in the application to stop responding for the specified period of time.

Perform a long-running operation on a new thread private void saveData(string fileName) { using (StreamWriter writer = new StreamWriter(saveDialog.FileName)) writer.WriteLine("First Name: {0}", firstName.Text); writer.WriteLine("Last Name: {0}", lastName.Text); Thread.Sleep(10000); MessageBox.Show("Member details saved", "Saved"); } private void saveMember_Click(object sender, RoutedEventArgs e) ... if (saveDialog.ShowDialog().Value) Thread workerThread = new Thread(() => this.saveData(saveDialog.FileName)); workerThread.Start();

The problem is that the security model implemented by WPF prevents any threads other than the thread that created a user interface object such as a control from accessing that object. This restriction prevents two or more threads from attempting to take control of the user input or modifying the data on the screen because this could result in corruption of your data.

Copy data from the user-interface thread to the background thread struct Member { public string FirstName { get; set; } public string LastName { get; set; } } private void saveData(string fileName, Member member) using (StreamWriter writer = new StreamWriter(fileName)) writer.WriteLine("First Name: {0}", member.FirstName); writer.WriteLine("Last Name: {0}", member.LastName); Thread.Sleep(10000); MessageBox.Show("Member details saved", "Saved");

private void saveMember_Click(object sender, RoutedEventArgs e) { private void saveMember_Click(object sender, RoutedEventArgs e) { ... if (saveDialog.ShowDialog().Value) Member member = new Member(); member.FirstName = firstName.Text; member.LastName = lastName.Text; Thread workerThread = new Thread( () => this.saveData(saveDialog.FileName, member)); workerThread.Start(); }

An alternative approach is to create a BackgroundWorker object. private void bnSync_Click( object sender, EventArgs e ) { // Create a background thread BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += new DoWorkEventHandler( bw_DoWork ); bw.RunWorkerAsync(); } You specify the method that a BackgroundWorker object runs by subscribing to the DoWork event. The DoWork event expects you to provide a DoWorkEventHandler delegate