Visual Basic Fundamental Concepts

29
Visual Basic Fundamental Concepts

description

Visual Basic Fundamental Concepts. Integrated Development Enviroment. Generates startup form for new project on which to place controls. Features toolbox from which controls can be selected & placed on form. Features intellisense which prompts for completion of statement or object reference. - PowerPoint PPT Presentation

Transcript of Visual Basic Fundamental Concepts

Page 1: Visual Basic Fundamental Concepts

Visual Basic Fundamental Concepts

Page 2: Visual Basic Fundamental Concepts

Integrated Development Enviroment

Generates startup form for new project on which to place controls.

Features toolbox from which controls can be selected & placed on form.

Features intellisense which prompts for completion of statement or object reference.

Provides immediate syntax checking. Provides online debugging, displaying intermediate

values. Provides HELP feature.

Page 3: Visual Basic Fundamental Concepts

Toolbox and Controls

Label control Textbox

control

Button control

Toolbox

Page 4: Visual Basic Fundamental Concepts

Basic Controls

Label : Displays information through its Text property.

Textbox : Information Entry, Display, and Transfer through its Text Property. Use name of textbox to access information or respond to Gotfocus event.

Button : Information Display through its Text Property. Use name of button to respond with subroutine to its Click event.

Page 5: Visual Basic Fundamental Concepts

Event Driven Programming

Before event driven programs, typical applications were executed by the computer under control of the application.

With event driven programs, the operating system detects user interactions (mouse click, tab key, etc.), passes them to the application, and a control responds by running a subprogram.

Page 6: Visual Basic Fundamental Concepts

Controls and Associated Events

Control EventForm Load event occurs when form is

loaded into computer memory

Button Click event occurs when user clicks button with mouse

Textbox Lostfocus event occurs when user tabs out of box

Page 7: Visual Basic Fundamental Concepts

Click event for Avg button

Private Sub cmdAvg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAvg.Click

'Compute average and store in text property of tbAvg control

tbAvg.Text = (CInt(tbNum1.Text) + CInt(tbNum2.Text)) / 2

tbNum1.Select()

tbNum1.SelectionStart = 0

tbNum1.SelectionLength = tbNum1.Text.Length

tbNum2.Clear()

lblStep1.Visible = True

lblStep2.Visible = False

End Sub

Page 8: Visual Basic Fundamental Concepts

Variables and Data Types

Variables are assigned values from a data type

VB data types include String, Integer, Double, and Boolean

A Dim statement is used to declare a variable and the data type from which its values are assigned

Page 9: Visual Basic Fundamental Concepts

Syntax of Dim and Assignment Statements

Dim– Dim <variable> as <data-type>

Dim avg as double Dim nmbr as integer Dim flag as boolean

Assignment– <variable> = <expression>

avg = (nmbr1 + nmbr2) / 2 flag = false

Page 10: Visual Basic Fundamental Concepts

Variables and Assignment Statements

Dim message As String ‘declares string variable 'Assign string constant to messages 1 and 2

‘Assignment statement : <variable> = <expression>

message = "Enter numbers to be “ & _ “averaged in 1st and 2nd textboxes“

lblStep1.Text = message message = "Click Average button" lblStep2.Text = message

Page 11: Visual Basic Fundamental Concepts

Creating a New Project

Open Visual Studio 2005

Click Project after

Create:

Page 12: Visual Basic Fundamental Concepts

Choosing Project Language, Template, and Project Name

Select Visual Basic

– Windows Windows

Application Enter Project

Name (Lab1 in example)

Click OK

Page 13: Visual Basic Fundamental Concepts

View StartupForm andSet Properties

Page 14: Visual Basic Fundamental Concepts

Problem Analysis & Application Development

Analyze Problem– Example :

Find averages of pairs of numbers, number1 and number2

– Input : number1, number2– Process : average = (number1 + number2) / 2– Output : average

Page 15: Visual Basic Fundamental Concepts

Project Implementation & Software Development

Design Solution– Choose forms and controls

Select label – textbox pairs for – Input : number1 and number2 and – Output : average

Select buttons to launch processes– Average

Write code to implement process Gets number1 and number2 values Computes average = (number1 + number2) /2 Displays average

Page 16: Visual Basic Fundamental Concepts

• Select Label Control from Toolbox

• Click on Form to position Label – drag if desired.

• Click on Textbox Control from Toolbox

• Click on Form to position Textbox to right of Label

Page 17: Visual Basic Fundamental Concepts

• Name textbox tbNumber1

• Holding shift key down, select label and textbox together

• Copy with Ctrl-C

• Paste with Ctrl-V (for Number2 entry.

• Select button Control and position below label – textbox pair

• Paste again below button for output.

• Set text properties of labels and the button

• Name textbox and button controls

• Write code for button click event – double click on button to generate subroutine shell

• Label – textbox pair

• Copy with Ctrl-C

Page 18: Visual Basic Fundamental Concepts

• Controls after selection

• Now set text properties of labels and the button

• Name textbox and button controls

• Write code for button click event – double click on button to generate subroutine shell

Page 19: Visual Basic Fundamental Concepts

• Controls after setting text properties of labels and the button

• Name textbox and button controls

• Name of textbox for output

• Write code for button click even by double clicking button

• Button code should

• Compute & display average

• Clear input boxes and select first box

Page 20: Visual Basic Fundamental Concepts

• Write code for button click event by double clicking button

• Button code performs following:

• Computes average

• Displays average

• Clears input boxes and selects first box

Page 21: Visual Basic Fundamental Concepts

• Test Program in debugger by switching back to design view

• Click debugger button

Page 22: Visual Basic Fundamental Concepts

• Copy Screen to Copy Buffer by doing a Print Screen

• Paste onto a Word Document

• Type your name & turn in.

Page 23: Visual Basic Fundamental Concepts

Data types and Statement Types

Data

TypeValues Statement

TypeSyntax

Integer 3, 211, -42, . Declaration Dim <variable> as <datatype>

Double 3.17, 1.414 Assignment <variable> = <exp>

String “Hello”, “Bob” Selection If <condition> then

<statement_list>

End if

Boolean True, False Repetition For <variable> = <start> to <end>

<statement_list>

Next

Page 24: Visual Basic Fundamental Concepts

Controls and Associated Events

Control EventForm Load event occurs when form is loaded into computer

memory

Button Click event occurs when user clicks button with mouse

Textbox Lostfocus event occurs when user tabs out of box

Listbox SelectedIndexChanged occurs when user clicks a list item with mouse

Page 25: Visual Basic Fundamental Concepts

Problem Analysis & Application Development

Analyze Problem– Example :

Find average of list of numbers incrementally by– Keeping track of number of numbers– Keeping track of sum of numbers– Computing average after each number– Displaying numbers in a listbox

Page 26: Visual Basic Fundamental Concepts

Average of List of Grades

Page 27: Visual Basic Fundamental Concepts
Page 28: Visual Basic Fundamental Concepts

If statement for Control

If statement performs an action (<statement_list>) if a condition (<condition>) is true.

Syntax :– If <Condition> then

<statement_list>

– End if

Page 29: Visual Basic Fundamental Concepts

If statement Example with Else part

Syntax with Else– If <Condition> then

<statement_list1>– Else

<statement_list2>– End if

Example :– If hours > 40 then

overtimeHours = hours – 40– Else

overtimeHours = 0– End If