introduction to windows piprogramm ing · standard controls c# programming: from problem analysis...

18
1 Copyright © 2007 Robinson College of Business, Georgia State University David S. McDonald Director of Emerging Technologies Tel: 404-413-7368; e-mail: [email protected] 8 Introduction to Windows P i 8 Programming David McDonald, Ph.D. Director of Emerging Technologies C# Programming: From Problem Analysis to Program Design 2nd Edition Chapter Objectives Learn about graphical user interfaces Become aware of some elements of good design Use C# and Visual Studio to create Wi d b d li ti C# Programming: From Problem Analysis to Program Design Windows-based applications

Upload: others

Post on 28-Sep-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

1

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

8 Introduction to Windows P i8 Programming

David McDonald, Ph.D.Director of Emerging Technologies

C# Programming: From Problem Analysis to Program Design 2nd Edition

Chapter Objectives

Learn about graphical user interfaces

Become aware of some elements of good design

Use C# and Visual Studio to create Wi d b d li ti

C# Programming: From Problem Analysis to Program Design

Windows-based applications

Page 2: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

2

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

Chapter Objectives (continued)

Create Windows forms and be able to h fchange form properties

Add control objects such as buttons, labels, and text boxes to a form

W k th h i l

C# Programming: From Problem Analysis to Program Design

Work through a programming example that illustrates the chapter’s concepts

Graphical User Interfaces Windows applications also look different from console applicationsInterface: front end of a program√Visual image you see when you run a

program Graphical user interface (GUI) includes:√

C# Programming: From Problem Analysis to Program Design

√Menus√Text in many different colors and sizes√Other controls (pictures, buttons, etc.)

Page 3: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

3

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

Windows ApplicationsReference 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)

C# Programming: From Problem Analysis to Program Design

Base class (second class)• public class Form1 : Form

Derived classes inherit from base class

Windows Applications (continued)Text√ A property for setting/getting title bar caption√ Can be used in constructor√ Can be used in constructor

Windows forms/controls offer many properties including Text, Color, Font, and LocationExecution begins in Main( ) method√Main( ) is located in Program.cs file for the application√ Call to Run( ) method places application in process

C# Programming: From Problem Analysis to Program Design

√ ( ) p pp ploop

Page 4: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

4

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

// Windows0.cs Author: McDonaldusing System.Windows.Forms; // Line 1namespace Windows0{

public class Form1 : Form // Line 2{

public Form1( ) // Line 3

New namespace referenced

C t t

Base class

public Form1( ) // Line 3{

Text = "Simple Windows Application"; // Line 4}static void Main( )

{Form1 winForm = new Form1( ); // Line 5

Constructor

Sets title bar caption

C# Programming: From Problem Analysis to Program Design

( );Application.Run(winForm); // Line 6

}}

}Starts

process loop

Windows Application (continued)

Output generated

from Windows0 application

C# Programming: From Problem Analysis to Program Design

Figure 8-1 Windows-based form

Page 5: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

5

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

Elements of Good Design Appearance matters √Human-computer interaction (HCI) p ( )

researchDesign considerations√Consistency √Alignment

C# Programming: From Problem Analysis to Program Design

√Avoid Clutter √Color √Target Audience

Use Visual Studio to Create Windows-based Applications

Windows Application

templateBrowse

to location to store

Select File New

Project

C# Programming: From Problem Analysis to Program Design

your workName

Figure 8-2 Visual Studio New Windows application

Page 6: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

6

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

Windows-based Applications

PropertiesWindow

Switch between

Design and Code view using View

menu

C# Programming: From Problem Analysis to Program Design

Design ViewToolbox

Figure 8-3 Initial design screen

Windows-based Applications

PropertiesAuto-hide

pushpin

Dynamic Help

Solution Explorer

C# Programming: From Problem Analysis to Program Design

Figure 8-4 Dockable windows

Page 7: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

7

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

Windows Forms

Extensive collection of Control classes Top level window for an application isTop-level window for an application is called a Form Each control has large collection of properties and methods √Select property from an alphabetized list √ p p y p

(Properties window)√Change property by clicking in the box

and selecting or typing the new entry

Windows Form Properties

Alphabetical

Events

Properties Property value

Categorized

C# Programming: From Problem Analysis to Program Design

Figure 8-5 Properties window

Page 8: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

8

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

Windows Form Properties (continued)

C# Programming: From Problem Analysis to Program Design

Windows Form EventsAdd 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

Page 9: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

9

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

Windows Form Properties

Events button

selected

C# Programming: From Problem Analysis to Program Design

Figure 8-6 Form1 events

Windows Form – Closing Event

Code automatically added to register eventthis.Closing += new

S t C tM d l C lE tH dlSystem.ComponentModel.CancelEventHandler(this.Form1_Closing);

Code automatically added for method headingprivate void Form1_Closing(object sender,

System.ComponentModel.CancelEventArgs e){}

C# Programming: From Problem Analysis to Program Design

}You add statement to event-handler method bodyMessageBox.Show("Hope you are having fun!");

Page 10: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

10

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

Simple Windows Application New with Visual Studio 2005, the IDE separates the source code into three separate filessource code into three separate files√ Form1.cs: Normally this is the only one you edit√ Form1.Designer.cs: Holds the auto-generated

code√ Program.cs: Contains the Main( ) method, where

execution always begins

C# Programming: From Problem Analysis to Program Design

Form1.cs and Form1.Designer.cs both include partial class definitions for the Form1 class

Windows Form Events (continued)

Expand Form1.cs node to reveal the Form1.Designer.cs

file

C# Programming: From Problem Analysis to Program Design

Figure 8-7 Solution Explorer window

Page 11: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

11

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

Controls Controls are all classes

√Button, Label, TextBox, ComboBox, MainMenu, ListBox, CheckBox, RadioButton, and MonthCalendar

Each comes with its own predefined properties and methods

Each fires events

C# Programming: From Problem Analysis to Program Design

Each fires events

Each is derived from the System.Windows.Forms.Control class

Controls (continued)

DotsDots indicate

other classes

are derived from the

class

C# Programming: From Problem Analysis to Program Design

Figure 8-9 Control class hierarchy

Page 12: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

12

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

Standard Controls

C# Programming: From Problem Analysis to Program Design

Figure 8-10 Windows Forms controls

Controls (continued)Two procedures to place controls

√ From Toolbox, double-click on control or drag and√ From Toolbox, double click on control or drag and drop

Move, resize, and delete controls

Format controls

√ Align controls

C# Programming: From Problem Analysis to Program Design

√Make same size

√ Horizontal and vertical spacing

Page 13: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

13

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

Properties of the Control Class

C# Programming: From Problem Analysis to Program Design

Methods of the Control Class

C# Programming: From Problem Analysis to Program Design

Page 14: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

14

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

Controls

C# Programming: From Problem Analysis to Program Design

Figure 8-11 GUI controls

Label Objects

Provides descriptive text or labels for other controlsInstantiate objectInstantiate object

Label labelName = new Label( );Add control to Form

this.Controls.Add(labelName);Set property values (some from Control class)√ Text; TextAlign; Font; Location

C# Programming: From Problem Analysis to Program Design

√ Text; TextAlign; Font; Location

Page 15: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

15

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

Creating a TaxApp

C# Programming: From Problem Analysis to Program Design

Properties set for the Form container

Creating a TaxApp Form

Add Label

objects to Form

object, then

format

C# Programming: From Problem Analysis to Program Design

Figure 8-12 Formatting Label objects

Page 16: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

16

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

Adding Labels to TaxApp Form

Add Label objects, then set their properties using thetheir properties using the

Properties window (View Properties window)

C# Programming: From Problem Analysis to Program Design

TextBox ObjectsUsed to enter data or display text during run time √ Used for both input and output

I t ti t bj tInstantiate objectTextBox textBoxName = new TextBox( );

Add control to Form this.Controls.Add(TextBoxName);

Interesting properties√MultiLine, ScollBars, MaxLength, PasswordChar,

C# Programming: From Problem Analysis to Program Design

CharacterCasing

Page 17: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

17

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

TextBox Objects (continued)

C# Programming: From Problem Analysis to Program Design 33

Adding TextBox Objects to TaxApp Form

Add TextBox objects, then set their property

values

C# Programming: From Problem Analysis to Program Design

Page 18: Introduction to Windows PiProgramm ing · Standard Controls C# Programming: From Problem Analysis to Program Design Figure 8-10 Windows Forms controls Controls (continued) ¾Two procedures

18

Copyright © 2007Robinson College of Business, Georgia State UniversityDavid S. McDonald Director of Emerging TechnologiesTel: 404-413-7368; e-mail: [email protected]

ButtonEnables 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 toas 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)

C# Programming: From Problem Analysis to Program Design

• Text, Enabled, Focused, TabIndex

Adding Button Objects to TaxApp Form

Add Button objects, then set their property

values

C# Programming: From Problem Analysis to Program Design