8. WinForms Applications Writing native Windows programs Overview - - PowerPoint PPT Presentation

8 winforms applications
SMART_READER_LITE
LIVE PREVIEW

8. WinForms Applications Writing native Windows programs Overview - - PowerPoint PPT Presentation

8. WinForms Applications Writing native Windows programs Overview Windows Applications Events and event handlers Layered (tiered) model of software Focus Form designer and controls Dialog boxes and forms Delegates


slide-1
SLIDE 1
  • 8. WinForms

Applications

Writing native Windows programs

slide-2
SLIDE 2

Overview

Windows Applications Events and event handlers Layered (tiered) model of software Focus Form designer and controls Dialog boxes and forms Delegates Visual Inheritance

slide-3
SLIDE 3

Windows vs Console apps

A console application has a very limited user-

interface

Keyboard for input Console window (text only) for output

Compared to this a Windows program provides

a much richer range of interaction methods

On-screen buttons, scroll bars, lists etc. Multiple windows to categorize and simplify output

and input

The penalty for this is a more complex

application structure

slide-4
SLIDE 4

Windows and Events

Console programs follow

an InputProcessOutput model

Easy to program

By contrast, a Windows

program can have many different forms of input

Programmer must

  • rchestrate the various

sources

Each source of input fires

‘events’ the program must react to Input Process Output

Software

slide-5
SLIDE 5

Event handlers

Each source of events can kick off the execution

  • f a block of code called an event handler

An event handler is simply a Sub that is called

automatically by Windows in response to some external interaction

Typing into a text control etc. ‘Clicking’ on a control Selecting an item in a list

Often, the event incorporates some additional

information – which key was pressed, the exact location of the mouse cursor etc.

This information is passed in the form of parameters to the

event handler

slide-6
SLIDE 6

Layers of software

Because of the wide range of stimuli (events) a program

has to react to, the structure of a Windows application is more complex

One strategy for simplifying the structure is to try to keep

application specific code and user-interface code separate

Application specific code is usually the set of classes that do the

actual job required of the program, often called the ‘business classes’

User-interface code accepts user input and commands and

directs the business classes according to these

These two separate parts of a system can be thought of

a ‘tiers’ or ‘layers’ in a system

User-interface tier comes between the user and the business tier

and is responsible for interpreting user interactions and effecting the appropriate changes in the business tier

slide-7
SLIDE 7

A 2-Tier Application

User- Interface Tier Business Tier

Handles User Interactions Implements Business Rules

slide-8
SLIDE 8

WinForms basics

A Form is a top-level Window that is hosted and

managed by the Windows operating system and which receives external interactions in the form of events

Controls in Visual Basic are toolbox items that can be

placed on a form to act as user-interface elements (or widgets)

Windows considers a control to be another type of

Window – a rectangular area of the screen that can receive the focus (explained later) and events

This confuses the issue, and so the Visual Basic term

‘Control’ is a better description

slide-9
SLIDE 9

The Focus

The system mouse can, by virtue of its pointer, be used to

direct input to any visible window on a screen

The input is necessarily simple – movements, clicks, drags

The keyboard is a richer form of input, but there is no

natural way of directing its input

To get over this, the Windows system registers which

window has the current focus

In a text-oriented control, this is the cursor and acts as the

insertion point for typed characters from the keyboard

In other controls, the focus changes the control’s appearance

slightly to indicate which control is currently receiving keyboard events

Focus can be changed using the Tab key on the keyboard TheTabIndex of a control indicates its place in the tab order

slide-10
SLIDE 10

Events and event handlers

Events can originate from

Forms or controls on forms System hardware (e.g. the real-time clock) Other windows programs

Events are usually received by forms

Normal event-handlers are Subs on a form Event-handlers can be coded into other modules, but

this involves more work

A form-based event handler is usually associated with

a specific event from a specific control

It has two parameters, indicating the object that sent

the event (sender) and an object containing related information (e)

slide-11
SLIDE 11

Example event handler

Private Sub txtName_KeyPress(ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyPressEventArgs) _ Handles txtName.KeyPress Dim ch As Char ch = e.KeyChar() e.Handled = True End Sub

The above handler is set to receive KeyPress events from a

text box named txtName

The sender parameter is simply a reference to this text box As a KeyPress event handler, you can rightly expect that

the other parameter will contain information on the key pressed – which key, whether Shift or Ctrl was pressed, etc.

slide-12
SLIDE 12

Event lists

A typical control can

source a wide range

  • f events under

different circumstances

Only events for which

a handler is defined will have any effect

List of potential events from a TextBox control

slide-13
SLIDE 13

The Visual Studio form designer

The Form-Designer performs a complex task –

synchronizing the graphical design of a form with the code

The actual ‘Form’ is a class that inherits from the Form

  • class. The graphical designer is simply a different view
  • f it – a graphical class configuration panel

As controls are added, properties changes and

design wizards executed, the form code is updated to reflect the changes

Each control is a variable, and code to initialize it Each property change is a statement that assigns a new

value to a property

A double click on a control generates a skeleton event

handler

slide-14
SLIDE 14

Windows controls

The standard WinForms toolbox contains a

wide range of controls. e.g.

Text input: TextBox, RichTextBox, ComboBox Mouse input: CheckBox, OptionButton,

ListBox, ScrollBar

Display and/or organize: Label, PictureBox,

Panel

Command: Button, Menu, Timer

All can be configured by the properties

window or in code

slide-15
SLIDE 15

Dialog boxes

A dialog box is a form that performs complete

  • perations

Buttons provided to confirm (usually OK) or cancel

(usually Cancel) the operation

Operation typically involves a number of data values,

which after editing them, the user selects OK or Cancel

A VB Form has AcceptButton and CancelButton

properties that can refer to these buttons

This allows the DialogResult to reflect which button was

pressed – True for OK, False for Cancel

A dialog box provides a Modal operation – while it

is in operation, the calling code has to wait for it

This imposes a level of control over the user (1 dialog at

a time)

slide-16
SLIDE 16

Forms

A dialog box is a special type of form More generally, any number of forms can be in

  • peration simultaneously

This can be a more difficult scenario to control

Generally, use a single form to input and display

the properties of an object and to initiate its methods

A number of strategies can be taken to maintain

  • control. e.g.

The object can ‘own’ (initiate, interrogate and dispose

  • f) a form and initiate its own display, update etc.

A form can ‘contain’ an object, and update and

display it (more suitable where one form is used to browse a collection of objects

slide-17
SLIDE 17

Delegates and event handlers

A delegate is defined as a type of Sub

A stand-in for real subs Delegate defines name, parameters and return type, but does

not contain code

The purpose of this is to be able to include a call to a sub

in code before the sub has been written

Why would you do this? – think of what an event handler is

Once a delegate type has been defined, a real sub can

be assigned to it (just like a variable)

Then, a call to the delegate will be a call to the sub assigned to it e.g. We can now assign a Sub (that matches AlarmCall) to AC

Delegate Sub AlarmCall(ByVal AlarmMessage As String) Dim AC As AlarmCall

slide-18
SLIDE 18

Example of delegation

Class AlarmClock Private myTime As Date Private myMessage As String 'Here is the delegate declaration... Private myResponse As AlarmCall Public Sub New(ByVal AlarmTime As Date, _ ByVal Message As String, _ ByVal SubToCall As AlarmCall) myTime = AlarmTime myMessage = Message 'Assign a Sub to the delegate... myResponse = SubToCall End Sub Public Sub Go() Do If TimeOfDay >= myTime Then 'A call to the delegate method... myResponse(myMessage) Exit Do End If Loop End Sub End Class

Using the delegate included here as a template, we can write a sub that will say what happens when an alarm goes off.

slide-19
SLIDE 19

Example of delegation (contd.)

'This one displays a message on screen... Sub AlarmMessageBox(ByVal aMessage As String) MessageBox.Show(aMessage) End Sub 'This one changes a form’s caption... Sub AlarmCaption(ByVal aMessage As String) MainForm.Caption = aMessage End Sub 'This one sends an email... Sub AlarmEmail(ByVal aMessage As String) SendMail("me@mydomain.com", "Alarm", aMessage) End Sub

Any of these three subs can be assigned to the delegate, and will be called when the alarm goes

  • ff. Note – all

have same parameters as the delegate.

Sub Main() Dim AC As AlarmClock = _ New AlarmClock(TimeValue("10:00:00"),_ "Go to airport", AddressOf AlarmEmail) AC.Go() End Sub

slide-20
SLIDE 20

Visual Inheritance

Code Inheritance allows a new class to be

based on an existing class

A Form is a class, designed in code like any

  • ther class (form designer just displays it in a

more convenient form for us)

Visual Inheritance allows us to take an initial

form design (in a form builder window) and extend it by adding new controls

As a result, the new form’s code will inherit from

the existing one.

slide-21
SLIDE 21

Visual Inheritance

V i s u a l I n h e r i t a n c e Form design and any code defined (including event handlers) for the

  • riginal form is inherited

by the new one.

slide-22
SLIDE 22

Summary

Windows applications more complex than console ones,

and so often a layered model is used to simplify

Forms respond to events, and event handlers are written

to define what happens when an event is received

The Form designer allows controls to be placed on forms

and configured with no program code

Dialogs are special (modal) types of form Delegates are used to pre-define a type of subroutine Visual Inheritance allows a form design to be extended

within the form designer