8 Introduction to Windows Programming C Programming From























































- Slides: 55

8 Introduction to Windows Programming C# Programming: From Problem Analysis to Program Design 1

Chapter Objectives • Differentiate between the functions of Windows applications and console applications • Learn about graphical user interfaces • Become aware of some elements of good design • Use C# and Visual Studio to create Windowsbased applications C# Programming: From Problem Analysis to Program Design 2

Chapter Objectives (continued) • Create Windows forms and be able to change form properties • Add control objects such as buttons, labels, and text boxes to a form • Work through a programming example that illustrates the chapter’s concepts C# Programming: From Problem Analysis to Program Design 3

Contrasting Windows and Console Applications • Console applications – Each line in Main( ) executed sequentially – then the program halts • Windows applications – Once launched, sits and waits for an event – Sits in a process loop • Event: notification from operating system that an action, such as the user clicking the mouse or pressing a key, has occurred – Write event-handler methods for Windows apps C# Programming: From Problem Analysis to Program Design 4

Graphical User Interfaces • Windows applications also look different from console applications • Interface: front end of a program – Visual image you see when you run a program • Graphical user interface (GUI) includes: – Menus – Text in many different colors and sizes – Other controls (pictures, buttons, etc. ) C# Programming: From Problem Analysis to Program Design 5

Windows Applications • Reference and import System. Windows. Forms namespace • Class heading definition – Includes not only the class name, but a colon followed by another class name • Derived class (first class) • Base class (second class) • public class Form 1 : Form • Derived classes inherit from base class C# Programming: From Problem Analysis to Program Design 6

Windows Applications (continued) • Text – A property for setting/getting title bar caption – Can be used in constructor • Windows forms/controls offer many properties including Text, Color, Font, and Location • Execution begins in Main( ) method – Main( ) is located in Program. cs file for the application – Call to Run( ) method places application in process loop C# Programming: From Problem Analysis to Program Design 7

// Windows 0. cs Author: Doyle using System. Windows. Forms; // Line 1 New namespace Windows 0 namespace referenced { Base class public class Form 1 : Form // Line 2 { Constructor public Form 1( ) // Line 3 { Text = "Simple Windows Application"; // Line 4 Sets } title bar caption static void Main( ) { Form 1 win. Form = new Form 1( ); // Line 5 Application. Run(win. Form); // Line 6 } Starts process } loop } C# Programming: From Problem Analysis to Program Design 8

Windows Application (continued) Output generated from Windows 0 application Figure 8 -1 Windows-based form C# Programming: From Problem Analysis to Program Design 9

Elements of Good Design • Appearance matters – Human-computer interaction (HCI) research • Design considerations – – – Consistency Alignment Avoid Clutter Color Target Audience C# Programming: From Problem Analysis to Program Design 10

Use Visual Studio to Create Windows-based Applications Select File New Project Windows Application template Name Browse to location to store your work Figure 8 -2 Visual Studio New Windows application C# Programming: From Problem Analysis to Program Design 11

Windows-based Applications Switch between Design and Code view using View menu Properties Window Design View Toolbox Figure 8 -3 Initial design screen C# Programming: From Problem Analysis to Program Design 12

Windows-based Applications (continued) pushpin Properties Auto-hide Solution Explorer Dynamic Help Figure 8 -4 Dockable windows C# Programming: From Problem Analysis to Program Design 13

Windows Forms • Extensive collection of Control classes • Top-level window for an application is called a Form • Each control has large collection of properties and methods – Select property from an alphabetized list (Properties window) – Change property by clicking in the box and selecting or typing the new entry C# Programming: From Problem Analysis to Program Design 14

Windows Form Properties Events Alphabetical Categorized Property value Properties Figure 8 -5 Properties window C# Programming: From Problem Analysis to Program Design 15

Windows Form Properties (continued) C# Programming: From Problem Analysis to Program Design 16

Windows Form Events • Add code to respond to events, like button clicks • From the Properties window, select the lightening bolt (Events) – Double-click on the event name to generate code • Registers the event as being of interest • Adds a heading for event-handler method C# Programming: From Problem Analysis to Program Design 17

Windows Form Properties (continued) Events button selected Figure 8 -6 Form 1 events C# Programming: From Problem Analysis to Program Design 18

Windows Form – Closing Event • Code automatically added to register event this. Closing += new System. Component. Model. Cancel. Event. Handler (this. Form 1_Closing); • Code automatically added for method heading private void Form 1_Closing(object sender, System. Component. Model. Cancel. Event. Args e) { } • You add statement to event-handler method body Message. Box. Show("Hope you are having fun!"); C# Programming: From Problem Analysis to Program Design 19

Simple Windows Application • New with Visual Studio 2005, the IDE separates the source code into three separate files – Form 1. cs: Normally this is the only one you edit – Form 1. Designer. cs: Holds the auto-generated code – Program. cs: Contains the Main( ) method, where execution always begins • Form 1. cs and Form 1. Designer. cs both include partial class definitions for the Form 1 class C# Programming: From Problem Analysis to Program Design 20

Windows Form Events (continued) Expand Form 1. cs node to reveal the Form 1. Designer. cs file Figure 8 -7 Solution Explorer window C# Programming: From Problem Analysis to Program Design 21

Controls • Controls are all classes – Button, Label, Text. Box, Combo. Box, Main. Menu, List. Box, Check. Box, Radio. Button, and Month. Calendar • Each comes with its own predefined properties and methods • Each fires events • Each is derived from the System. Windows. Forms. Control class C# Programming: From Problem Analysis to Program Design 22

Controls (continued) Dots indicate other classes are derived from the class Figure 8 -9 Control class hierarchy C# Programming: From Problem Analysis to Program Design 23

Standard Controls Figure 8 -10 Windows Forms controls C# Programming: From Problem Analysis to Program Design 24

Controls (continued) • Two procedures to place controls – From Toolbox, double-click on control or drag and drop • Move, resize, and delete controls • Format controls – Align controls – Make same size – Horizontal and vertical spacing C# Programming: From Problem Analysis to Program Design 25

Properties of the Control Class C# Programming: From Problem Analysis to Program Design 26

Methods of the Control Class C# Programming: From Problem Analysis to Program Design 27

Controls Figure 8 -11 GUI controls C# Programming: From Problem Analysis to Program Design 28

Label Objects • Provides descriptive text or labels for other controls • Instantiate object Label label. Name = new Label( ); • Add control to Form this. Controls. Add(label. Name); • Set property values (some from Control class) – Text; Text. Align; Font; Location C# Programming: From Problem Analysis to Program Design 29

Creating a Tax. App Properties set for the Form container C# Programming: From Problem Analysis to Program Design 30

Creating a Tax. App Form Add Label objects to Form object, then format Figure 8 -12 Formatting Label objects C# Programming: From Problem Analysis to Program Design 31

Adding Labels to Tax. App Form Add Label objects, then set their properties using the Properties window (View Properties window) C# Programming: From Problem Analysis to Program Design 32

Text. Box Objects • Used to enter data or display text during run time – Used for both input and output • Instantiate object Text. Box text. Box. Name = new Text. Box( ); • Add control to Form this. Controls. Add(Text. Box. Name); • Interesting properties – Multi. Line, Scoll. Bars, Max. Length, Password. Char, Character. Casing C# Programming: From Problem Analysis to Program Design 33

Text. Box Objects (continued) C# Programming: From Problem Analysis to Program Design 34

Adding Text. Box Objects to Tax. App Form Add Text. Box objects, then set their property values C# Programming: From Problem Analysis to Program Design 35

Button • Enables user to click button to perform task – If button has event-handler method and is registered as an event to which your program is planning to respond, event-handler method is called automatically when button clicked • Button object’s properties, methods, and events – Inherits from Control (Table 8 -2 & 8 -3, slides 25 & 26) • Text, Enabled, Focused, Tab. Index C# Programming: From Problem Analysis to Program Design 36

Adding Button Objects to Tax. App Form Add Button objects, then set their property values C# Programming: From Problem Analysis to Program Design 37

Adding Button Objects to Tax. App Form (continued) Click to see list of events Double-click to create an event-handler method Figure 8 -14 Events C# Programming: From Problem Analysis to Program Design 38

Adding Button Objects to Tax. App Form (continued) • When you double-click on event, an eventhandler method is created: private void btn. Compute_Click(object sender, System. Event. Args e) { } • AND registers click event: this. btn. Compute. Click += new System. Event. Handler (this. btn. Compute_Click); C# Programming: From Problem Analysis to Program Design 39

Adding Button Objects to Tax. App Form (continued) private void btn. Compute_Click(object sender, System. Event. Args e) { string in. Value; double purchase. Amt, percent, ans; in. Value = txt. Purchase. Text; purchase. Amt = Int 32. Parse(in. Value); in. Value = label 5. Text; //in. Value previously declared as string in. Value = in. Value. Remove(in. Value. Length-1, 1); percent = double. Parse(in. Value) / 100; percent = (double. Parse(label 5. Text. Remove(label 5. Text. Length-1, 1))) / 100; ans = (purchase. Amt * percent) + purchase. Amt; txt. Total. Due. Text = String. Format("{0: C}", ans). To. String(); C# 40 } Programming: From Problem Analysis to Program Design

Tax. App Form Figure 8 -15 Tax calculator output C# Programming: From Problem Analysis to Program Design 41

Temp. Agency Application Example Figure 8 -16 Problem specification for Temp. Agency C# Programming: From Problem Analysis to Program Design 42

Temp. Agency Application Example (continued) C# Programming: From Problem Analysis to Program Design 43

Temp. Agency Application Example (continued) Figure 8 -17 Prototype for Temp. Agency example C# Programming: From Problem Analysis to Program Design 44

Temp. Agency Application Figure 8 -18 Class diagrams for Temp. Agency example C# Programming: From Problem Analysis to Program Design 45

Algorithm for Temp. Agency Figure 8 -19 Pseudocode for the Employee class for the Temp. Agency example C# Programming: From Problem Analysis to Program Design 46

Test Data for Temp. Agency C# Programming: From Problem Analysis to Program Design 47

Properties for Temp. Agency C# Programming: From Problem Analysis to Program Design 48

C# Programming: From Problem Analysis to Program Design 49

C# Programming: From Problem Analysis to Program Design 50

C# Programming: From Problem Analysis to Program Design 51

Temp. Agency Example Figure 8 -20 First user interface for the payroll application C# Programming: From Problem Analysis to Program Design 52

Temp. Agency Example (continued) Figure 8 -21 Output produced when the Calculate button is clicked C# Programming: From Problem Analysis to Program Design 53

Chapter Summary • Windows versus console applications • Graphical user interfaces • Elements of good design • Visual Studio with Windows-based applications – Drag-and-drop construction C# Programming: From Problem Analysis to Program Design 54

Chapter Summary (continued) • Properties – Getters – Setters • Controls as objects – Buttons – Labels – Text. Box C# Programming: From Problem Analysis to Program Design 55