You are on page 1of 89

INTRODUCTION TO EVENT HANDLERS

AND WINDOWS FORMS APPLICATIONS


Chapter 03

OBJECTIVES
With this topic, the students should be able to:

Compare command-line interfaces and forms-based interfaces


Design the user interface for a Windows application
Create a Windows Forms Application project using Visual Studio
Use Visual Studio to create and configure control instances
Use controls to create the user interface for a Windows Forms Application
project
Create code for a Windows Forms Application project

COMPARING COMMAND-LINE
AND FORM-BASED INTERFACES
command-line interface
It display textual prompts and gets end user input one line at a time.

form-based visual interface


It is one in which the end user interacts with an application via a visual form
containing buttons, text boxes, check boxes, scroll bars, and other visual
objects.

Button
Label

Command-line interface

Form-based visual interface

DESIGNING A USER INTERFACE


Principles of a good user interface
Principle

Description

Control

The end user should always control the application, rather than the
other way around.

User-friendliness

The interface should help the end user accomplish tasks and not call
attention to itself.
Example
Too many different fonts or images can be distracting

Intuitiveness

The interface should follow a direct style that proceeds logically.


Example
It an end user needs to complete consecutive steps to accomplish
a
task, the steps should be grouped together and proceed one after

DESIGNING A USER INTERFACE


Principles of a good user interface
Principle

Description

Consistency

The interface should be conceptually, linguistically, visually, and functionally


consistent.
Example
The font style used should be consistent throughout the interface.
The visual objects on a form should have the same or similar shapes
and sizes.

Feedback

The interface should provide clear and immediate feedback to the end user.
Example
If the user adds a record to a file, the interface should inform the end
user that the record was added.
If the end user makes an error when entering data, the interface
should communicate the cause of the error and suggest possible
solutions to the problem.

DESIGNING A USER INTERFACE


Principles of a good user interface
Principle

Description

Graphics

The interface should not be cluttered with graphics that serve no


useful purpose.
Example
An icon of a stop sign or an exit sign is an intuitive metaphor to
use as
a button to end the application.

Input

The interface should minimize situations in which the end user needs
to switch input modes from the mouse to the keyboard and back
again.

Label to display the image filename

Label prompt
User Interface
File name:

Button to
display a
dialog box

Center
Normal

Button to
resize the
image

Stretch
Clear
Exit

Picture box to display the graphical image

Button to
clear the
image
Button to
exit the
application

DESIGNING A USER INTERFACE


control instances
These are visual objects created on a form.

Principles of control design


Principle

Description

Alignment

Control instances appearing in a column should be aligned vertically.


Control instances appearing in a row should be aligned horizontally.

Balance

Control instances should be distributed evenly around the form.

DESIGNING A USER INTERFACE


Principles of control design
Principle

Description

Appropriate color

Use soft colors, such as blue or gray, for most forms.


Use bright colors, such as red, only for emphasis.
The foreground and background colors should have adequate
contrast.
Similar visual objects should have similar colors.

Function grouping

Control instances that have a similar purpose should be grouped


together.

Consistent sizing

Control instances should be consistent in size.


Example
When a form has multiple buttons, they should all be the same
size.

control instances are not


balanced on the form
control instances are not
aligned
numerous hard-to-read fonts
add unnecessary
complexity to the form
background image is distracting
and serves no useful
purpose

A poor user interface

aligned control instances


simplified fonts and colors

An improved user interface

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

Visual Basic Windows


project types selected

Windows Forms Application


template selected

Display small
icons enabled

Solution name

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

Differences between Windows Forms Application projects and Console


Application projects:

The only visual parts of a Console Application project are the textual
messages that appear in a Command Prompt window. Windows Forms
Application projects are graphical applications having one or more windows
with which the end user interacts.
Console Application projects are procedural. Windows Forms Application
projects are event-driven.
The startup object for a Console Application project is typically a procedure
named Main. The startup object for a Windows Forms Application project is
typically a form.

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

The Anatomy of a Windows Forms Application Project

Purpose of the windows in the Windows Forms Application Project


Solution Explorer window
It is used to manage the files that make up the solution and its project(s).

Windows Forms Designer window


It is where a forms visual interface gets created.

Toolbox window
It is a tool window containing the visual tools, called controls.
These are special category of classes that appear on a form.

Properties window
It provides a visual way to set the properties for an object at design time.
Example
A form has a property named Text containing a string that appears in the forms title
bar.

Solution Explorer

Toolbox

Windows Forms Designer

Properties window

The Solution Explorer

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

It is used to manage the files in a solution.


My Project folder
It contains an AssemblyInfo.vb file, which describes the assembly, and folders named
bin and obj, which store the compiled project.

A Windows Forms Application projects contains references to the following


assemblies related to forms and drawing:
System namespace
All projects, no matter their type, contain a reference to this namespace.

System.Drawing namespace
It is used by Windows Forms Application projects to render (draw) the form and the visual
objects appearing on the form.

System.Windows.Forms namespace
It contains those classes that make up the visual forms and the objects created on those
forms.

Show All Files


button selected

Project support
files are the same

Assembly references

Form files

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

The Windows Forms Designer and the Toolbox


Forms

A window that makes up a Windows Forms Application

Two parts of a form:


user interface (form)
It is made up of a rectangular window containing such visual objects as buttons and
boxes called control instances created from controls appearing in the Toolbox.

code behind the form

Windows Forms Designer (visual designer)


It is a tool used to create a forms interface in a visual way.

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

The Windows Forms Designer and the Toolbox


Ways to activate the Windows Forms Designer

1. Select the form name in the Solution Explorer, and then select View, Designer on
the menu bar.
2. Right-click the form name in the Solution Explorer, and then select View Designer
from the context menu.
3. Double-click the form name in the Solution Explorer.

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

The Windows Forms Designer and the Toolbox


General characteristics of a form:
title bar (optional and located on the top)

It can have an optional caption.


The caption can contain spaces and other special characters, however, the caption
should fit on the visible title bar.

control box (optional and located along the right side of the title bar)
It has buttons to minimize, maximize, and restore a maximized form to its original size.
It can be made visible or hidden.

Minimize button (located on the leftmost)


It is used to hide the form, causing it to appear as a button on the taskbar.

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

The Windows Forms Designer and the Toolbox


General characteristics of a form:

Restore / Maximize button (toggle and appears as the center button in the control box)
Maximize button
It causes the form to maximize, thus filling the desktop.
Restore button
It restores the form to its original size.

Close button
It closes the form or the application itself.

Menu (optional and appears below the forms title bar)


A form typically has a border. (may or may not be resizable)
Client area
It is the region inside a forms border, excluding the title bar and optional menu.
It is where the forms control instances appear.

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

The Windows Forms Designer and the Toolbox


System.Windows.Forms.Form class
It supports methods, properties, and events.
Methods

Close method
It closes the form, thereby unloading it from memory.
Hide method
It makes a form invisible, but the form is not unloaded from memory.
Show method
It displays a form.

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

The Windows Forms Designer and the Toolbox


System.Windows.Forms.Form class
Properties

AcceptButton property
It contains the name of a Button control instance.
Pressing the Enter key causes the Click event to fire for the button designated as the
Accept button.
There can be only one Accept button per form.
It is often called the Default button
BackColor property
It specifies the forms background color.
ControlBox property
If set to True, it appears on the forms title bar.
If set to False, it does not appear.
Rightmost button always appears and is enabled when the control box is visible.

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

The Windows Forms Designer and the Toolbox


System.Windows.Forms.Form class
Properties

MaximizeBox property
If set to True, the Restore/Maximize buttons are enabled.
If set to False, the buttons are disabled.
MinimizeBox property
It defines whether the Minimize button is enabled or disabled.
FormBorderStyle property
It is used to specify the appearance of the border surrounding a form and whether the for
is resizable.
None
No border surrounds the form
FixedSingle and Fixed3D
It causes the form to appear with a flat and three-dimensional border respectively.

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

The Windows Forms Designer and the Toolbox


System.Windows.Forms.Form class
Properties
FormBorderStyle property
FixedDialog

It appears the same to the end user as the setting FixedSingle.


It is used to display forms that must be closed before the end user can interact
with the other forms in the application.
Sizable
It configures the form so that it can be resized by dragging the forms border.
FixedToolWindow and SizableToolWindow
It causes the form to appear with a shorter title bar.
These are called tool windows.
Appearance is similar to the appearance of Visual Studio tool windows.

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

The Windows Forms Designer and the Toolbox


System.Windows.Forms.Form class
Properties

CancelButton property
It contains the name of the Button control instance designated as the Cancel button.
Pressing the Escape key causes the Click event to fire for the associated button.
Only one button on a form can be designated as the Cancel button.
Icon property
It contains the image that appears in the upper-left corner of the forms title bar.
Text property
It contains the caption that appears in the forms title bar.
Width and Height properties
It define the size of the form.
Values are expressed in pixels.

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

The Windows Forms Designer and the Toolbox


System.Windows.Forms.Form class
Properties

StarPosition property
It is used to specify where on the desktop the form will appear when it is displayed.
CenterScreen
The form is displayed in the center of the desktop.
Manuao
The position of the form is defined by the X and Y properties.
X and Y properties
It define the position of the form relative to the desktop.
Only used when the StartPosition property is set to Manual.
Values are expressed in pixels.

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

The Windows Forms Designer and the Toolbox


System.Windows.Forms.Form class
Events

Load event
It fires when a form is loaded into memory each time the application is run.
It is used to perform initialization tasks, such as displaying the current date on the
form.
FormClosing event
It fires just before a form is unloaded from memory.

Me keyword
It is a special identifier used to reference the current instance of a form.
Close method -> Me.Close()
It close the current form instance, thereby ending the application.

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

The Windows Forms Designer and the Toolbox


Toolbox

It contains all of the visual tools (controls) necessary to create a forms user
interface.
Creating a control instance on a form
Click the desired control in the Toolbox. The selected control appears highlighted. Then,
drag the outline of the control instance into the Windows Forms Designer to define its
position and size.
It is also possible to create a control instance by double-clicking the control in the
Toolbox. The control is created at the upper-left corner of the form, or adjacent to the
most recently created control instance.

Common Controls
tab expanded

Tabs collapsed

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

Introduction to the Properties Window


Property

It is used to store data for a class instance (object).

Four sections of the Properties window


list arrow (appears to the right of the Object drop-down box at the top of the
Properties window)
It is used to select an object such as a form or a control instance created on a form.
Object drop-down box
It displays the currently selected object

toolbar area
It contains buttons used to change the order in which the properties appear in the list
section and whether to display the properties or events for the selected object.

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

Introduction to the Properties Window


Four sections of the Properties window
list section
Name column
It contains the name of each property.
Value column

It contains the current value assigned to a property.

Description section (appears at the bottom of the window)


It contains a brief description of the selected property.
It can be hidden.

Categorized button
The properties are organized based on the purpose of the property.

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

Introduction to the Properties Window


Alphabetic button

The properties appear sorted in ascending alphabetic order.

Display properties
by category

Display properties
alphabetically
Display properties
Object drop-down box
Toolbar
Display events

List section

Name column

Value column
Description section

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

Configuring Textual and Hierarchical Properties


textual value

The property is edited by typing the desired value in the Value column
Example
Name property store a textual value

Hierarchical
The values are set using a drill-down interface.
Example
Location property has X and Y properties.
Size property has Width and Height properties.

Location property
expanded

Hierarchical property
collapsed

Hierarchical properties in the Properties window

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION
Three
color
Click list arrow to

Configuring Textual and Hierarchical Properties


palette tabs
Properties involving color

display color palette

Values are set using a color palette.


Example
BackColor property
ForeColor property
Custom tab
It is used to create custom colors.

Web tab
Lists those color supported by the Web.

System tab
It causes the color of a control instance to
be the same as the color defined by a
common Windows system property.

Color palette

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION
Click to display

Configuring Textual and Hierarchical Properties

Some properties have a fixed set of values from which one


value can be selected.
The desired value is selected from a drop-down list.
Example
SizeMode property
PictureBox property
Drop-down list

drop-down list

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

Configuring Textual and Hierarchical Properties


Some properties show the dialog box
Example
Font property

It display a dialog box, allowing the programmer to select the typeface, size,
and font attributes.

USING VISUAL STUDIO TO


CREATE WINDOWS FORMS
APPLICATION

Configuring Textual and Hierarchical Properties

Some properties have a visual editor allowing a programmer to set the


property.
Example
TextAlign proeperty
Label property

Property value is
an enumeration
Click the list
arrow to display
the visual editor
Visual Editor

Windows Forms
Designer active

frmMain selected in
Object drop-down box

ControlBox
property

Sizing handles
appear

FormBorderStyle
property
Setting properties for a form.

USING VISUAL STUDIO TO CREATE AND


CONFIGURE CONTROL INSTANCES
Toolbox and Windows Forms Designer
It is used together to create control instances on a form.

Creating and Deleting Control Instances


Two ways of creating the control instance on the form:
In the Toolbox, double-click the desired control.
In the Toolbox, click the desired control. Move the mouse pointer to the Windows
Forms Designer. The mouse pointer changes to a crosshair along with an icon of
the control. Click and drag the mouse to define the initial size and position of the
control instance. Release the mouse to create the control instance at its desired
position.

USING VISUAL STUDIO TO CREATE AND


CONFIGURE CONTROL INSTANCES
Creating and Deleting Control Instances
Deleting a control instance:
Click the control instance in the Windows Forms Designer and then press the
Delete key.
Right-click a control instance to select it, and then click Delete from the context
menu that appears.

Moving and Resizing Control Instances


Ways to move and resize the selected control instance:
Pressing the arrow keys moves the selected control instance in the direction of the
arrow.
Holding down the Control key while pressing the arrow keys moves the selected
control instance in the direction of the arrow.

USING VISUAL STUDIO TO CREATE AND


CONFIGURE CONTROL INSTANCES
Moving and Resizing Control Instances
Ways to move and resize the selected control instance:
Holding down the Shift key while pressing the arrow keys resizes the control
instance.
The mouse can be used to drag the sizing handles, which are the small boxes that
appear around the selected control instance.
Sizing handle do not appear if the AutoSize property is set to True.

The Height and Width properties can also be set using the Properties window,
thereby resizing the control instance.

USING VISUAL STUDIO TO CREATE AND


CONFIGURE CONTROL INSTANCES
Working with Multiple Control Instances
Ways to select multiple control instances:
Using the Pointer tool in the Toolbox, drag a rectangular region marking the
control instances to be selected.
While holding down the Shift key, click each control instance to select it.

Aligning Multiple Control Instances


Format menu
It has menu items used to align the selected control instances and appears only
while the Windows Forms Designer is active.

USING VISUAL STUDIO TO CREATE AND


CONFIGURE CONTROL INSTANCES
Aligning Multiple Control Instances
Different alignment commands appearing on the Format menu:
Align command
It aligns the left, right, top, or bottom margins of the selected control instances based on
the position of the active control instance.

Make Same Size command


As its name implies, it resizes the selected control instances to match the size of the
active control instance.

Horizontal Spacing and Vertical Spacing commands


It align the control instances with the form horizontally or vertically.

Center in Form command


It centers the selected control instances between either the top and bottom margins of
the form or the left and right margins.

USING VISUAL STUDIO TO CREATE AND


CONFIGURE CONTROL INSTANCES
Aligning Multiple Control Instances
Snap lines
It allows one control instance to be aligned with another.

Horizontal
snap line

Contro instance
being aligned
Vertical
snap line

INTRODUCTION TO VISUAL
STUDIO CONTROLS
Controls:
PictureBox control
It displays graphical images.

Label control
It displays text.

Button control
It performs a specific task when clicked by the end user.

OpenFileDialog control
It displays a dialog box, allowing the end user to select a file to open.

ToolTip control
It displays an informational pop-up message when the mouse hovers over another
control instance for a few seconds.

INTRODUCTION TO VISUAL
STUDIO CONTROLS
The System.Windows.Forms.Control Class
It is the base class for all visual controls.
Control Class
It supplies the basic functionality to get input from the keyboard and mouse and
to display output.

System.Windows.Forms
System.Windows.Forms.Control
System.Windows.Forms.ButtonBase
System.Windows.Forms.Button
System.Windows.Forms.Label
System.Windows.Forms.PictureBox
System.Windows.Forms.CommonDialog
System.Windows.Forms.FileDialog
System.Windows.Forms.OpenFileDialog
Hierarchical organization of control classes

INTRODUCTION TO VISUAL
STUDIO CONTROLS
Understanding the Name Property
Rules for naming identifiers:
Must begin with a letter and contain only letters, digits, and the underscore
character.
It must be unique.

INTRODUCTION TO VISUAL
STUDIO CONTROLS
Understanding the Name Property
Control and form prefixes
Control

Prefix

Button

btn

Label

lbl

OpenFileDialog

ofd

PictureBox

pic

ToolTip

tip

Form

frm

INTRODUCTION TO VISUAL
STUDIO CONTROLS
Understanding the Name Property
Common Errors

Click to collapse
Details section

Properties Window error dialog box.

INTRODUCTION TO VISUAL
STUDIO CONTROLS
Members of the Control Class
Syntax
System.Windows.Forms.Control class

Methods
Hide method
It makes the control instance invisible.

Show method
It makes the control instance visible.

Properties
Forecolor and BackColor properties
It define the text color and color appearing behind the text, respectively.

INTRODUCTION TO VISUAL
STUDIO CONTROLS
Members of the Control Class
Properties
Enable property
It stores values True or False, indicating whether the control instance will respond to
end user interaction.

Height and Width properties


It store the size of the control instance.

AutoSize property
If set to True, the control instance is resized based on its contents.

MinimumSize property
It defines the minimum size of the control instance.

MaximumSize property
It defines the maximum size of the control instance.

INTRODUCTION TO VISUAL
STUDIO CONTROLS
Members of the Control Class
Properties
Location property
It is used to set the position of the control instance on the form.

Name property
It is used to uniquely identify the control instance.

Text property
It contains the text that appears in the visible region of the control instance.

Visible property
It stores the values True or False and indicates whether the control instance is visible
or hidden from the end user.

INTRODUCTION TO VISUAL
STUDIO CONTROLS
Members of the Control Class
Events
Click event
It fires when the end user clicks the mouse on a control instance.

The Label Control


Syntax
System.Windows.Forms.Label class

INTRODUCTION TO VISUAL
STUDIO CONTROLS
The Label Control
Properties
BoderStyle property
It can be set to one of three constant values.
None
No border surrounds the label.
FixedSingle
A flat border surround the label.
Fixed3D
The control instance appears recessed.

Font property
It defines the typeface and the size of the text appearing in the label.

TextAlign property
It determines how the textual characters are aligned inside the region of the label.

INTRODUCTION TO VISUAL
STUDIO CONTROLS
The Button Control
Syntax
System.Windows.Forms.Button class

Properties
X, Y, Height, and Width properties
It define its position and size on the form.

Enabled property
If set to True, Windows fires a Click event when the end user clicks the button.
If set to False, the button text appears shaded, and Click events are not fired.

FlatStyle property
Flat
The button appears flat on the form.

INTRODUCTION TO VISUAL
STUDIO CONTROLS
The Button Control
FlatStyle property
Popup
The button appears raised when the mouse pointer hovers over the button at run
time.
Standard
The button appears raised off the forms surface.
System
The appearance of the control instance is the same as that of the one used by the
operating system.

Image property
It contains a picture that appears in the button along with the button text.

ImageAlign property
It is used to indicate where the image will appear within the buttons visible region.

INTRODUCTION TO VISUAL
STUDIO CONTROLS
The Button Control
Text property
It contains the text that appears in the buttons visible region.
It can also create a hot key for a button by inserting an ampersand character (&) in the
Text propertys value just before the character that should act as the hot key.
It is a set of keystrokes, such as Alt+C, which, when typed by the end user,
produces the same result as clicking the button.
Example
The string E&xit would cause Alt+X to be the hot key.

Events
Click event
Windows fires when the end user clicks the button.

INTRODUCTION TO VISUAL
STUDIO CONTROLS
The PictureBox Control
It can display JPEG, bitmap, and other image formats.
Syntax
System.Windows.Forms.PictureBox class

Properties
BorderStyle property
It defines the appearance of the border surrounding the control instance.

Image property
It contains the image displayed in the control instance.

ImageLocation property
It contains the name of the disk file where the image is stored.

INTRODUCTION TO VISUAL
STUDIO CONTROLS
The PictureBox Control
Properties
SizeMode property
It defines how the image appears inside the region of the control instance.
Normal
The image appears at the upper-left corner of the control instance and is not resized.
StretchImage
It causes the image to be resized such that it fills the region of the control instance.
AutoSize
The control instance resizes to fit the image.
CenterImage
It causes the image to be centered in the control instance.

Events
Click event
A PictureBox control instance can be made to work as if it were a Button control instance.

Control instance is
resized to fit image

Image appears at
its actual size

Image is stretched to
fill the control instance

Image appears centered


in control instance

Using the SizeMode property to scale images

INTRODUCTION TO VISUAL
STUDIO CONTROLS
The OpenFileDialog Control
It displays a dialog box, allowing the end user to select a file to open.

INTRODUCTION TO VISUAL
STUDIO CONTROLS
The OpenFileDialog Control
Method and property needed to use the OpenFileDialog control:
ShowDialog method
It displays the dialog box to the end user.

FileName property
It stores the filename selected by the end user.

Statement showing how to display an OpenFileDialog control instance named


ofdImages, and then write the filename to the Console window.
ofdImages.ShowDialog()
System.Console.WriteLine(ofdImages.FileName)

Invisible control instances


appear in a resizable component
Tray below the form

CREATING CODE FOR A


WINDOWS FORMS APPLICATION
Introduction to Class Blocks and Partial Classes
Statements showing the structure of a form named frmMain with a filename of
frmMain.vb.
Public Class frmMain
statements
End Class
Difference between a Class and Module blocks:
A Module block can have a procedure named Main marking the applications entry
point. A Class block can have a procedure named Main, it is not the applications
entry point.
Objects are created from templates called classes, and multiple objects (class
instances) can be created from the same class. Because a form is a class, multiple
forms (objects) can be created from the same class. Modules work indifferently. As

CREATING CODE FOR A


WINDOWS FORMS APPLICATION
Introduction to Class Blocks and Partial Classes
partial class
It is a class that is split across multiple physical files.

Two reasons why partial classes are useful:


Suppose that several developers were creating a complex class. Using partial
classes, each developer could work with a separate physical file, allowing the
other developers to work on the other parts of the class contained in other files.
When development is complete, all of the files that are part of the class can be
completed.
Visual Studio and Windows Forms Designer use partial classes. Visual Studio splits
the class for a form into two files. One file is managed entirely by the Windows
Forms Designer. Developer-created code appears in the other file. This division of
code prevents accidental modification or corruption of code generated by the
Windows Forms Designer.

CREATING CODE FOR A


WINDOWS FORMS APPLICATION
Introduction to Class Blocks and Partial Classes
Statements showing the structure of the frmMain.Designer.vb file:
Partial Public Class frmMain
Inherits System.Windows.Forms.Form
Windows Forms Designer generated code.
End Class
Partial keyword
It denotes that the class is a partial class.
The preceding class declaration also contains the following Inherits statement:
Inherits System.Windows.Forms.Form

CREATING CODE FOR A


WINDOWS FORMS APPLICATION
Introduction to Class Blocks and Partial Classes
derived class
It can inherit the members of another class called the base class.
The preceding statement indicates that the class named frmMain inherits from the
base System.Windows.Forms.Form class.

Automatically generated
code appears in the
frmMain.Designer.vb file

Show All Files


button selected

Procedures

frmMain.Designer.vb
file selected

CREATING CODE FOR A


WINDOWS FORMS APPLICATION
Introduction to Event Handlers
event handler
It is a procedure containing one or more Visual Basic statements that Windows
executes when the end user performs an action, such as clicking a button.

Code segment showing a Click event handler for a Button control instance
named btnDemo:
Private Sub btnDemo_Click( ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnDemo.Click
End Sub
Handles clause
It marks a procedure as an event handler.

Application
starts

User clicks a
button firing an
event

Application waits
for an event to
fire

Windows calls
event-handling
procedure

Event-handling
procedure
finishes

CREATING CODE FOR A


WINDOWS FORMS APPLICATION
Introduction to Event Handlers
Two ways to create event handlers automatically:
Double-clicking a control instance in the Windows Forms Designer activates the
Code Editor and creates the event handler for controls most commonly used
event.
Example
Double-clicking a Button control instance in the Windows Forms Designer
causes Visual Studio to create the Click event handler for that button.
Using the Code Editor along with the Class Name and Method Name drop-down
boxes.

btnDemo selected

Class Name drop-down


box displays the form
and control instances

Button control
instance selected

Events applicable to the


Button control appear in the
Method Name drop-down box

CREATING CODE FOR A


WINDOWS FORMS APPLICATION
Using Assignment Statements to Read and Write Properties
assignment statement
It is made up of a left side and a right side, separated by an equal sign (=).
The expression on the right side is evaluated, and the result is stored in the
expression on the left side.
Example:
A literal string value appears on the right side using the following statement:
Me.Text = Visual Basic
Result:
The value of the forms Text property appears in the forms title bar.

CREATING CODE FOR A


WINDOWS FORMS APPLICATION
Using Assignment Statements to Read and Write Properties
assignment statement
Example:
Storing values in the Top and Left properties of the PictureBox control
instance
named picCurrent:
picCurrent.Top = 0
picCurrent.Left = 0
Result:
It set the Top and Left properties to 0, thereby causing the upper-left corner of
the picture box to appear in the upper-left corner of the forms client area.

CREATING CODE FOR A


WINDOWS FORMS APPLICATION
Using Assignment Statements to Read and Write Properties
assignment statement
Example:
Using the literal values on the right side of an assignment statement, its
possible
for the properties to appear on the right side of an assignment
statement:
picCurrent.Width = Me.Width
picCurrent.Height = Me.Height
Result:
The width and height of the picture box is set to the width and height of the
form.

CREATING CODE FOR A


WINDOWS FORMS APPLICATION
Using Assignment Statements to Read and Write Properties
assignment statement
Example:
Properties storing Boolean values:
btnDemo.Visible = False
picCurrent.Enabled = True
Result:
The first statement sets the Visible property to False, making the control
instance invisible. The second statement sets the Enabled property to True,
causing the control instance to respond to events.

enumeration
It is a descriptive name used in place of a numeric value.

CREATING CODE FOR A


WINDOWS FORMS APPLICATION
Using Assignment Statements to Read and Write Properties
Statements illustrating how to set the SizeMode property of the picture box
named picDemo.
picDemo.SizeMode = PictureBoxSizeMode.CenterImage
picDemo.SizeMode = PictureBoxSizeMode.Normal
Possible enumeration values for the SizeMode property of the PictureBox
control:

AutoSize
CenterImage
Normal
StretchImage

CREATING CODE FOR A


WINDOWS FORMS APPLICATION
Using Assignment Statements to Read and Write Properties
Image property
It stores an image to be displayed in the PictureBox control

FromFile method
It reads an image from a disk file stores it as an Image object.

Statement that shows how to read an image by calling the FromFile method
of the System.Drawing,Image class, and store a reference to that Image
property of the PictureBox control instance named picDemo.
picDemo.Image =
System.Drawing.Image.FromFile(C:\House1.jpg)

CREATING CODE FOR A


WINDOWS FORMS APPLICATION
Using Assignment Statements to Read and Write Properties
To remove an image from a PictureBox control instance, set the object to
Nothing.
picCurrent.Image = Nothing
Show method
It displays the control instance.
Example
lblDemo.Show()

Hide method
It makes the control instance invisible.
Example:
lblDemo.Hide()

You might also like