You are on page 1of 28

Week 2 : Controls and Events

Sep 7, 2016

Juhee Kwon
juhee.kwon@cityu.edu.hk
IS Department, City University of HK

The object-oriented programming.


The concepts of classes, objects, properties, and events.

The steps for visual basic program design and development.


Visual Basic Objects
Visual Basic Events

Object-Oriented Programming

Procedural Programming
KM

KM

Check
Orders & inventories

Display Information

Display Information
Sales
Get an order

Manufacturing

CRM

Get an order

Get an order

Get an order

Send Ads

Get an orders
customer info

Get its
customer info.

Check inventories

Ship the product


To the customer

Ship the product


to a store

Sales

Manufacturing

Check
inventories

CRM

Send Ads

Ship the product


To the customer

Each program has its own structured

Each program conducts a series of business


operations.

operations.

An operation works as an object and class.


An object and class can be shared across operations

There are redundant operations.


(ex) C, Fortran, Basic

(ex) Visual Basic, Java, C++


CityU of HK - IS5311

Object-Oriented Programming (1) :VB


OO languages work with objects that have properties and

functions (called as events). Each object is based on a class.


Form

Label

Textbox

Button

CityU of HK - IS5311

Object-Oriented Programming (2)


Objects vs. Classes
o

Class = car

Object = Each individual car is an object.

Properties of car class= make, model, color, engine, year

Event/Method = start, stop, arrive, crash

Make : BMW
Model : 2 Series Coup
Color : Red

Year

CityU of HK - IS5311

: 2014

Object-Oriented Programming (3)


Properties equate to Descriptions
Color or size of a Form
Example:
o a person has a name and ID number

Event functions are like Verbs.


Typical events include Close, Show and Clear
Examples: Eat, walk, etc.

CityU of HK - IS5311

OOP: Visual Basic

CityU of HK - IS5311

1) Define/design the user interface, which include


objects from classes (e.g., textbox, button, label,

etc.), to communicate with users


2) Set the properties of the objects.

3) Write the Code to implement the event functions


associated with the objects.

CityU of HK - IS5311

Project Files
When you create a new project, several more files and folders are created
by the system A project folder includes bin, My Project and obj.

CityU of HK - IS5311

Project Files
File Type

File Name

Description

Visual Studio
Solution

Week 01 Tutorial.sln

Organizes projects, project items and


solution items into the solution.

Form Files:

Week01Form.vb

A form for creating Windows-based


applications by the code editor.

Resource File for


the Form:

Week01Form.resx

A resource file includes additional resources


such as images and audio files in a form.

Form Designer:

Week01Form.Designer.vb

A form created by the design view.

Project User
Options File:

Week 01
Tutorial.vbproj.user

It includes user-specific settings that you


have made in the Visual Studio user
interface.

Week 01
Tutorial.vbproj.user

It contains information about the project,


such as the application platform, version,
and related files.

Project Options
File:

CityU of HK - IS5311

10

Naming Rules and Conventions


Have a set of naming standards and always follow them.
No spaces, punctuation marks, or reserved words
Object Class

Prefix

Example

Button

Btn

ex) Btnexit

Label

lb

ex) Lbname

TextBox

Txt

ex) TxtName

Radio button

Rbtn

ex) RbtnRed

CheckBox

chk

ex) ChkVisable

PictureBox

Pbox

ex) PboxSea

ListBox

Lst

ex) Lst_fruits

11

The default startup object is Form1


The properties window shows the properties of Form1

CityU of HK - IS5311

12

VB Objects
o

Text Box

Button

Label

Group Box

CheckBox

RadioButton

PictureBox

CityU of HK - IS5311

13

Place objects from the Toolbox

CityU of HK - IS5311

14

After creating window

objects, set their


properties.
o Name
o Font
o Visible
o Border Style

o Positioning and Aligning


o Setting Tab Order

CityU of HK - IS5311

15

2) Set Properties - Text Box

Allowed for user input

Text property

What is displayed in text box

What user entered in text box

TextAlign property

Aligning a text in the text box

16

2) Set Properties

Not Allowed for user input


Label automatically resizes to show

the text of the label.


When the AutoSize property is set to

False, label can be resized manually.


BorderStyle : flat or 3D

Color :
Set a color in the properties window

CityU of HK - IS5311

17

2) Set Properties - Picture Box

Displays/contains an image

Include image files into the Resource directory

Set a image file to Image property

(ex) pboxPic.Image = Week02_Tutorial.My.Resources.Resources.cityu

Other properties

Sizemode : Normal, Autosize, etc.

pboxPic

18

2) Set Properties - Check Box

Allows the user to select or deselect items/options.

Checked property

Checked = True/False

Use the Text property for the text


you want to appear

next to the box.

CityU of HK - IS5311

19

2) Set Properties - Radio Button

User may select only one in any group.

Checked property

Checked = True/False

Text property

What is displayed next to

the radio button


(ex) Green, Blue, and Black

CityU of HK - IS5311

20

2) Set Properties - Group Box

Used as a container for other objects the Color box.

Improves readability by separating the objects into logical groups

CityU of HK - IS5311

3) Writing the Code


The OO program
oWhile the project is running, the user can perform

actions.
oEach action by the user causes an event method to
occur.

We have to write code for an event method


(procedure).
22

3) Writing the Code - Editor Window


VB will automatically name an event method as the

object name, an underscore(_) and the name of the


event.

CityU of HK - IS5311

23

3) Writing the Code Private Sub button1_click() Handles button1.Click

Name, can
be changed.

Identifies event

Private Sub Button_Press() Handles button1.Click

CityU of HK - IS5311

24

3) Writing the Code An event is an action, such as the user clicking on

a button
Usually, nothing happens in a Visual Basic

program until the user does something and raises


an event.
What happens is determined by statements inside

the event procedure.

CityU of HK - IS5311

25

3) Writing the Code - Assignment


Assigns a value to a property or variable in the edit window
Enclose text strings in quotation marks (" ")
Ex) txtBox.ForeColor =
txtBox.Visible = True
txtBox.Text = "Hello World

pboxPic.image =

Clear label or text box: lbMsg.text=, txtBox.text =

controlName.property =
CityU of HK - IS5311

setting
26

3) Writing the Code Close function


Functions always have parentheses.
To execute a method of an object you write: object.function()
(ex) me.close()

Current Form may be referenced as Me

me.close()

CityU of HK - IS5311

27

3) Writing the Code - Remark


Begins with an apostrophe ( ' )
o On a separate line from executable code
o At the right end of a line of executable code
Also known as Comment, used for documentation.

Non-executable
Automatically colored Green in Editor

'Display the Hello World message.

CityU of HK - IS5311

28