COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference:...

49
COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus

Transcript of COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference:...

Page 1: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

COS240 O-O Languages AUBG, COS dept

Lecture 33Title:

C# vs. Java(GUI Programming)

Reference: COS240 Syllabus

Page 2: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Lecture Contents:

• Contrast btw the functions of Windows applications and console applications

• GUI - graphical user interfaces• Windows forms and form properties• Control objects such as buttons, labels,

and text boxes to a form

Page 3: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Contrasting Windows and Console Applications

• Console applications– Each line in Main( ) method executed sequentially –

then the program halts – Method calls might branch to different locations in the

program, however control always returns back to Main– Program initiates interaction with the user by calling

the OS to get data using ReadLine() method– Program calls OS to output data through method calls

like WriteLine() or Write()• Console applications run IPO model of

computing process

Page 4: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Contrasting Windows and Console Applications

• Windows applications– Instead of the program executing sequential

statements from top to bottom, the application, once launched, sits in what is called a process loop and waits for an event

– Sits in a process loop, waiting for event to execute

• Windows applications run Event-driven model of a computing process

Page 5: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Contrasting Windows and Console Applications

• Event: notification from OS that an action, such as the user clicking the mouse or the user pressing a key, has occurred

• Instead of calling the OS with IO request as console applications, Windows applications receive messages from OS that event has occurred

• It is must to write methods, called Event Handlers to indicate what should be done when an event occurs

Page 6: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Graphical User Interface

• Windows applications also look different from console applications

• User Interface: front end of a program– Visual image you see when you run a program– Algorithmic functionality stays behind GUI– Often users of programs actually identify the

interface of the program itself, when in reality, the interface is just one facet of the program.

– Algorithmic complexity is hidden behind GUI.

Page 7: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Graphical User Interface

• Graphical user interface (GUI) includes:– Menus, labels, text boxes, list boxes, – Other controls (pictures, buttons, etc.)– Text in many different colors and sizes

• Controls are objects that can display and respond to user interaction.

• Controls are placed in a container, called form, which is an object as well. It is instance of a class derived from predefined class Form

Page 8: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Windows Based Applications

• Windows Forms

• Events

• Controls

Page 9: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Chapter 9

Introduction to Windows Programming

C# Programming:From Problem Analysis to Program Design

4th Edition

Page 10: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 10

Contrasting Windows and Console Applications

• Windows applications function differently from console applications.

• Windows applications look differently from console applications.

Page 11: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 11

Contrasting Windows and Console Applications by Functionality

• 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

Page 12: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 12

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.)

Page 13: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 13

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 Form1 : Form • Derived classes inherit from base class • No multiple inheritance within .NET languages

Page 14: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 14

Windows Applications (continued)• Text - property of the Form class

– 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

Page 15: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 15

// Windows0.cs Author: Doyleusing System.Windows.Forms; // Line 1namespace Windows0{ public class Form1 : Form // Line 2 { public Form1( ) // Line 3 { Text = "Simple Windows Application"; // Line 4 } static void Main( ) { Form1 winForm = new Form1( ); // Line 5 Application.Run(winForm); // Line 6 } }}

New namespace referenced

Constructor

Base class

Sets title bar caption

Starts process

loop

Page 16: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 16

Windows Application (continued)

Figure 9-1 Windows-based form

Output generated

from Windows0 application

Page 17: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 17

Elements of Good Design

• Appearance matters – Human-computer interaction (HCI) research

• Design considerations– Consistency

– Alignment

– Avoid clutter

– Color

– Target audience

Page 18: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 18

Use Visual Studio to Create Windows-Based Applications

Windows Forms

Application template

Browse to

location to store

your work

Name

Figure 9-2 Visual Studio New Windows application

Select File New

Project

Page 19: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 19

Windows-Based Applications

Properties Window

Toolbox

Figure 9-3 Initial design screen

Design View

Page 20: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 20

Windows-Based Applications (continued)

Figure 9-4 Dockable windows

Page 21: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Windows Based Applications

•Windows Forms–Properties–Events

Page 22: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 22

Windows Forms• Extensive collection of Control classes • Top-level window for an application is called a Form • Each control has 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 at design time.

• Each control has collection of events.

Page 23: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 23

Windows Form Properties

Properties

Figure 9-5 Properties window

Property value

Page 24: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Windows Form Properties (change values at run time)

• Can set properties using program statements– Table 9-1 shows

properties set using Properties window

• Selecting Code on View menu shows associated code

C# Programming: From Problem Analysis to Program Design 24

Table 9-1 Form1 property changes

Page 25: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Windows Form Events

C# Programming: From Problem Analysis to Program Design 25

Figure 9-6 Form events

Page 26: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Inspecting the Code Generated by Visual Studio

• Three source code files ending with a .cs extension are part of the application

C# Programming: From Problem Analysis to Program Design 26

Figure 9-7 Solution Explorer window

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

file

Page 27: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 27

Simple Windows Application

• IDE separates the source 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

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

Page 28: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Inspecting the Code - Form1.cs

• Number of namespaces automatically added, including System.Windows.Forms

• Constructor calls InitializeComponent( ) methodpublic Form1( )

{

// Required for Windows Form Designer support.

InitializeComponent( );

}

• This is the file where event handler methods will be placed

C# Programming: From Problem Analysis to Program Design 28

Page 29: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Inspecting the Code - Form1.Designer.cs

• InitializeComponent( ) method included here• #region Windows Form Designer generated code

preprocessor directive– // do not modify the contents of this method with the

Code Editor

– Keyword “this.” precedes property name• Refers to current instance of the class

– #endregion // Ends the preprocessor directive

C# Programming: From Problem Analysis to Program Design 29

Page 30: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

InitializeComponent( ) Method

BackColor = Color.FromArgb (((Byte)(255)),

((Byte)(224)), ((Byte)(192)));

ClientSize = new Size(392, 373);

Font = new Font("Arial", 12F, FontStyle.Bold,

GraphicsUnit.Point, ((Byte)(0)));

ForeColor = Color.Blue;

Location = new Point(30, 30);

Margin = new Padding(4);

MaximizeBox = false;

Name = "Form1";

StartPosition = FormStartPosition.CenterScreen;

Text = "First Windows Application";

• Some of the auto generated code in the method– Added as

default values for properties or from changing property values

C# Programming: From Problem Analysis to Program Design 30

Page 31: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Windows Based Applications

•Events

Page 32: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 32

Windows Form Events

• Add code to respond to events, like button clicks

– Code goes into Form1.cs file

• From the Properties window, select the lightning 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

Page 33: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 33

Windows Form Properties (continued)

Events button

selected

Figure 9-6 Form1 events

Page 34: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 34

Windows Form – Load Event• Code automatically added to register event

• Code automatically added for method heading private void Form1_Load(object sender, EventArgs e)

{

}

• You add statement to event-handler method body this.BackColor = Color.Azure;

Page 35: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 35

Windows Form – FormClosing Event

• Code automatically added to register event

• Code automatically added for method headingprivate void Form1_FormClosing(object sender,

FormClosingEventArgs e)

{

}

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

Page 36: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Running the Windows Application

• No changes needed in the file that has Main( )

• Run like you do console applications (F5 or Ctrl+F5)

C# Programming: From Problem Analysis to Program Design 36

Figure 9-8 Output produced when the Close button causes the event-handler method to fire

Review FirstWindows Example

Page 37: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Windows Based Applications

•Controls

Page 38: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 38

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

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

Page 39: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 39

Controls (continued)

Dots indicate

other classes

are derived from the

class

Figure 9-9 Control class hierarchy

Page 40: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 40

Standard Controls

Figure 9-10 Windows Forms controls

Page 41: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 41

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

Page 42: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 42

Properties of the Control class

Table 9-2 Systems.Windows.Forms.Control class properties

Page 43: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Properties of the Control class (continued)

C# Programming: From Problem Analysis to Program Design 43

Table 9-2 Systems.Windows.Forms.Control properties (continued)

Page 44: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Methods of the Control class

• Control class has over 75 properties and over 100 methods– Not all are useful for every class that derives from it

C# Programming: From Problem Analysis to Program Design 44

Table 9-3 Systems.Windows.Forms.Control methods

Table 9-3 includes a short list of some

of the many methods.

Explore MSDN documentation

for more

Page 45: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

C# Programming: From Problem Analysis to Program Design 45

Controls (continued)

Figure 9-11 GUI controls

Page 46: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Practical Tasks

• Write a Windows based application – pure empty form with no any user added forms, controls or events

Page 47: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Practical Tasks

• Write a Windows based application – pure empty form with modified Text property at design time using Properties window

Page 48: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Practical Tasks• Write a Windows based application – pure

empty form with two event handlers:

• Event Load – when the form gets loaded– To modify Text property and BackColor

property at run time

• Event FormClosing – when the form gets closed – prior to app termination– Call Message.Show() method

Page 49: COS240 O-O Languages AUBG, COS dept Lecture 33 Title: C# vs. Java (GUI Programming) Reference: COS240 Syllabus.

Thank You For

Your Attention