Top Banner
Introduction to Windows Programming
43
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 4.C#

Introduction to Windows Programming

Page 2: 4.C#

Chapter Objectives

Differentiate between the functions of Windows applications and console applications

Learn about graphical user interfacesBecome aware of some elements of good designUse C# and Visual Studio to create Windows-

based applications

Page 3: 4.C#

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

Page 4: 4.C#

Windows Application Basics

Windows Forms is the event base smart-client component of the .NET Framework.

Application run locally on users' computers.Once launched

Set of managed libraries that enable common application tasks such as reading and writing to the file system. 

A form is a visual surface on which you display information to the user. You commonly build Windows Forms applications by placing controls on forms and developing responses to user actions, such as mouse clicks or key presses. A control is a discrete user interface (UI) element that displays data or accepts data input.

Page 5: 4.C#

Graphical User Interfaces

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 6: 4.C#

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

Page 7: 4.C#

Windows Applications (continued)

Text A property for setting/getting title bar caption

Name Unique name for all controls

Windows forms/controls offer many properties including Text, Color, Font, and Location,Size

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 8: 4.C#

using System.Windows.Forms; // Line 1namespace Windows0{ public class Form1 : Form // Line 2 { public Form1( ) // Line 3 {

InitializeComponent();

Text = "Simple Windows Application"; // Line 4 }

}}

New namespace referenced

Constructor

Base class

Sets title bar caption

Starts process

loop

Page 9: 4.C#

Windows Application (continued)

Figure 8-1 Windows-based form

Output generated

from sample from

application

Page 10: 4.C#

Elements of Good Design

Appearance matters Human-computer interaction (HCI) research

Design considerations Consistency Alignment Avoid Clutter Color Target Audience

Page 11: 4.C#

Use Visual Studio to Create Windows-based Applications

Windows Application

template

Browse to

location to store

your work

Select File New

Project

Name

Figure 8-2 Visual Studio New Windows application

Page 12: 4.C#

Windows-based Applications

Properties Window

Design View

Toolbox

Switch between

Design and Code view using View

menu

Figure 8-3 Initial design screen

Page 13: 4.C#

Windows-based Applications (continued)

Figure 8-4 Dockable windows

PropertiesAuto-hide

Solution Explorer

pushpin

Page 14: 4.C#

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 , Events Select property from an alphabetized list

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

selecting or typing the new entry

Page 15: 4.C#

Windows Form Properties

PropertiesProperty value

Figure 8-5 Properties window

Categorized

Alphabetical

Events

Page 16: 4.C#

Windows Form Properties (continued)

Page 17: 4.C#

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

Page 18: 4.C#

Windows Form Properties (continued)

Events button

selected

Figure 8-6 Form1 events

Page 19: 4.C#

Windows Form – Closing Event

Code automatically added to register eventthis.Closing += new

System.ComponentModel.CancelEventHandler (this.Form1_Closing);

Code automatically added for method headingprivate void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e){}

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

Page 20: 4.C#

Simple Windows Application New with Visual Studio 2010, the 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 21: 4.C#

Windows Form Events (continued)

Figure 8-7 Solution Explorer window

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

file

Page 22: 4.C#

Controls Controls are all classes

Button, Label, TextBox, ComboBox, MainMenu, ListBox, CheckBox, RadioButton, and DateTimePicker …

Each comes with its own predefined properties and methods

Each fires events

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

Page 23: 4.C#

Controls (continued)

Dots indicate

other classes

are derived from the

class

Figure 8-9 Control class hierarchy

Page 24: 4.C#

Standard Controls

Figure 8-10 Windows Forms controls

Page 25: 4.C#

Creating a form

Properties set for the Form container

Page 26: 4.C#

Sample Form with Controls

Figure 8-11 GUI controls

Page 27: 4.C#

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 28: 4.C#

Properties of the Control Class

Page 29: 4.C#

Methods of the Control Class

Page 30: 4.C#

Label Objects

Provides descriptive text or labels for other controls

Instantiate objectLabel labelName = new Label( );

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

Set property values (some from Control class) Text; TextAlign; Font; Location

Page 31: 4.C#

Adding Labels to FormAdd Label objects, then set their properties using the Properties

window (View Properties window)

Page 32: 4.C#

TextBox Objects

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

Instantiate objectTextBox textBoxName = new TextBox( );

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

Interesting properties MultiLine, ScollBars, MaxLength,

PasswordChar, CharacterCasing

Page 33: 4.C#

TextBox Objects (continued)

Page 34: 4.C#

Adding TextBox Objects to Form…

Add TextBox objects, then set their property

values

Page 35: 4.C#

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 to respond, event-handler method is called automatically when button clicked

Button object’s properties, methods, and events

Inherits from Control

Text, Enabled, Focused, TabIndex

Page 36: 4.C#

Adding Button Objects to Form

Add Button objects, then set their property

values

Page 37: 4.C#

Adding Button Objects to Form (continued)

Figure 8-14 Events

Click to see list of events

Double-click to create an event-handler method

Page 38: 4.C#

Add other controls to form

Combo boxMenu strip (Call Sample Salary Form)List boxDate Time pickerCheck boxRadio buttonCheck box listError provider

Page 39: 4.C#

Sample Salary Calculator

Page 40: 4.C#

Timer Control

A Timer control raises an event at a given interval.

If you need to execute some code after certain interval of time continuously, you can use a timer control.

Windows Forms have a Timer control that can be used at design time as well as at run-time

Page 41: 4.C#

Properties

Enabled Gets or sets whether the timer is running.

Interval Gets or sets the time, in milliseconds, before the Tick event

is raised relative to the last occurrence of the Tick event.

Page 42: 4.C#

Methods and Event

Method Protected method OnTick Raises the Tick event. Public method Start Starts the timer. Public method Stop Stops the timer.

Event Tick

Occurs when the specified timer interval has elapsed and the timer is enabled.

Page 43: 4.C#

Question ??