fundamentals of programming in vb 1

Upload: virgil-titimeaua

Post on 05-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 Fundamentals of Programming in VB 1

    1/31

    Fundamentals of

    Programming in Visual Basic

    Part 1Robert Burtch

    Surveying Engineering Department

    Ferris State University

    VISUAL BASIC OBJECTS

    VB programs display Windows style

    screen called a form

    Contains boxes and buttons called controls

    Forms and controls called objects

  • 7/31/2019 Fundamentals of Programming in VB 1

    2/31

    VISUAL BASIC OBJECTS To start click on Microsoft Visual Studio.NET

    option Opens Visual Studio start page

    START

    PROJECT VB.Net also known asapplication, solution orproject

    Each project saved as series of

    files and subfolders in it own

    folder

    Start project:

    click on File|New|Project

    Alternatively, click New Project

    icon

  • 7/31/2019 Fundamentals of Programming in VB 1

    3/31

    START PROJECT Opens New Project

    dialog Select Visual Basic

    Projects as Project

    Type

    Windows Application

    as Template

    Select name

    default

    WindowsApplication

    with number

    Select Location

    recommend special

    folder

    Click OK this will open the

    initial VB.NET screen

    INITIAL VB SCREEN

  • 7/31/2019 Fundamentals of Programming in VB 1

    4/31

    INITIAL VB SCREEN Menu bar

    Displays commands used with VB File, Edit, Project, Format,

    Debug, etc. Like other windows programs, but some specific to VB

    Toolbar Collection of icons that carry out standard operations

    Little information rectangle that pops up called a tooltip

    Main Area Holds Windows Form Designer

    Large stippled Form Window, or Form, becomes Window onexecution

    Solution Explorer Window Sometimes used to open programs stored on hard drive

    Properties Window Used to change how objects look and read

    Toolbox Controls that can be placed on Form

    TEXT BOX CAPABILITIES

    Double click text box icon

    Opens rectangle with sizinghandles in center of form

    If handles are displayed objectis said to be selected

    Rectangle can be resized bymoving mouse to one of thehandles

    Rectangle can be moved byholding down the left mousekey anywhere within theobject

  • 7/31/2019 Fundamentals of Programming in VB 1

    5/31

    CREATING TEXT BOX

    Alternative

    approach

    Click on text box

    icon

    Move mouse to

    any place in form

    Hold down left

    mouse button and

    drag a diagonal to

    create rectangle

    Textbox Cursor

    TEXT BOX

    Alphabetic tab display Categorized tab

    display

    To access Properties window, click on Properties

    window icon in Toolbar or click on the Properties

    window or press F4

  • 7/31/2019 Fundamentals of Programming in VB 1

    6/31

    TEXT BOX

    Click on Text option in Properties menu In right column, add desired text

    Text changes in both the Settings box and

    text box

    TEXT BOX Highlight property ForeColor to change

    color of text (alternatively,press Shift-

    Ctrl+F to go to first property beginning

    with F)

  • 7/31/2019 Fundamentals of Programming in VB 1

    7/31

    TEXT BOX Highlight Font property with single click

    of mouse Click on ellipsis () to open Font dialog

    box

    PROGRAM EXECUTION

    To execute

    program, click on

    Start icon

    Example ofexecuted form

    Text displayed insingle line to right hides text to left

  • 7/31/2019 Fundamentals of Programming in VB 1

    8/31

    TEXT BOX

    In Properties window, turnon Multiline option

    Result is that

    words wrap

    Can make Text

    box read only:

    Properties|ReadOnly

    Property|True

    TEXT BOX

    Can create multiple text boxes in the

    form

    One with cursor is the active box and this

    is called the focusfocus

  • 7/31/2019 Fundamentals of Programming in VB 1

    9/31

    COMMAND BUTTON

    Double click on Button icon in

    Toolbox

    Creates a Button on form

    Can be manipulated, like the text

    box

    Button

    Cursor

    COMMAND BUTTON Click on Caption in Properties window

    and type in a new caption for the

    command button

    Text added to button at same time added

    to Properties window

  • 7/31/2019 Fundamentals of Programming in VB 1

    10/31

    COMMAND BUTTON

    Command buttonlook at execution

    Button appears to

    move in and out

    Will add code tobutton

    Can underline parts ofcommand button text by

    adding ampersand to text

    Here, P is called the

    access key pressing

    Alt+P activates program

    like clicking on command

    button

    LABELS

    Double click on label icon

    Places label at center of form

    Label Cursor

  • 7/31/2019 Fundamentals of Programming in VB 1

    11/31

    LABELS

    Alignment canbe changed by

    double clicking

    Alignment

    property in

    Properties

    window

    Executing

    program doesnothing to label

    Just sits there

    User cannot

    change what a

    label displays

    LISTBOX

    Click ListBox icon

    Click and drag

    location of listbox

    ListBox displayedon Form

  • 7/31/2019 Fundamentals of Programming in VB 1

    12/31

    NAME PROPERTY

    Every control has name property Used in code to refer to control

    Name must start with letter

    Max of 215 characters

    Can include numbers and underscore

    Cannot include punctuation or spaces

    SUGGESTED NAMING CONVENTION

    Hungarian notation: 3-letter prefix identifying the type of

    control

    FrmProgram1frmForm

    txtAddresstxtText box

    lstOutputlstList box

    lblInstructionslblLabel

    btnComputeTotalbtnButton

    EXAMPLEPREFI

    X

    CONTROL

  • 7/31/2019 Fundamentals of Programming in VB 1

    13/31

    HELP WALKTHROUGH

    Dynamic help

    Displays list of topics appropriate for what youare working on

    To Invoke: Alt | H | H

    Help Index

    Alphabetized list of all help topics

    To Invoke: Alt | H | I

    FONTS Default Microsoft Sans Serif

  • 7/31/2019 Fundamentals of Programming in VB 1

    14/31

    FONTS Courier New

    Fixed-width font

    Useful to arrange

    columns

    Wingdings

    Assorted smallpictures and symbols

    AUTOHIDE

    Pushpin vertical

    AutoHide disabled

    Pushpin horizontal

    AutoHide enabled

    Allows you to make more room for Main area of screen by

    minimizing tool windows

  • 7/31/2019 Fundamentals of Programming in VB 1

    15/31

    When opening/closing

    programs use Open

    Solution and Close

    solution from File

    Menu

    Do not use Open and

    Close

    USEFUL PROPERTIES

    BackColor

    Specified background color for form orcontrol

    Visible

    When False, causes objects to disappearwhen program is run

    Can be made to reappear with code

    Enabled

    When False, restricts its use

    Appears grayed and cannot receive focus

  • 7/31/2019 Fundamentals of Programming in VB 1

    16/31

    VISUAL BASIC EVENTS

    Event

    Action by user such as clicking a control

    or pressing a key

    Programmer writes code to do something

    when event occurs

    3 steps in creating a VB program

    1. Create interface generate, position, size

    objects

    2. Set properties configure appearance of

    objects

    3. Write code that executes an event

    VB.NET EVENTS Properties of controls changed using following

    form

    controlName.property = setting

    controlName = name of control

    Property = one of controls properties

    Setting = any valid setting for control property

    Examples

    txtBox.ForeColor=Color.Red Changes characters in text box to red txtBox.Text = Hello

    Words Hello displayed in text box

    btnButton.Visible=True Makes button visible

  • 7/31/2019 Fundamentals of Programming in VB 1

    17/31

    EVENT PROCEDURE

    Statements executed when even occurs

    written in block of code called eventprocedure

    First line of Event Procedure has form(sometimes called Procedure-DeclarationStatement)Private Sub objectName_event (ByVal sender As

    System.Object, ByVal e As System.EventArgs)Handles objectName.event

    Often simplified as

    Private Sub ojbectName_event() HandlesobjectName.event

    statements

    End Sub

    KEYWORDS

    Words like Private, ByVal, As,

    Handles and End keywords or

    reserved words

    VB.NET automatically capitalizes first letters

    of keywords and displays it in blue

  • 7/31/2019 Fundamentals of Programming in VB 1

    18/31

    EVENT PROCEDURE

    WALKTHROUGH

    RedTextbtnRed

    (blank)TexttxtSecond

    (blank)TexttxtFirst

    Demonstratio

    n

    TextForm1

    SETTINGPROPERTYOBJECT

    Create a Form with 2 text boxes and one button

    Change the properties of the controls

    EVENT PROCEDUREWALKTHROUGH

  • 7/31/2019 Fundamentals of Programming in VB 1

    19/31

    EVENT PROCEDURE

    WALKTHROUGH Code window

    Right click anywhere on Main area and

    select View Code from pop-up

    Page tabs between Code window and Form

    Design window

    Method Name Box

    Class Name Box

    EVENT PROCEDUREWALKTHROUGH Rectangular area corresponds to

    collapsed block of code

    Hover cursor to see information about it

  • 7/31/2019 Fundamentals of Programming in VB 1

    20/31

    EVENT PROCEDURE

    WALKTHROUGH

    To enter code, double click on control Program will automatically take you to

    Code window and will enter the first and

    last line for the event procedure

    First line: Private Sub

    Last line: End Sub

    Will write code between these two lines

    Alternatively, click on Class Name box

    and select control

    EVENT PROCEDURE WALKTHROUGH Class Name Box

    Then select desired option in Method

    Name Box

  • 7/31/2019 Fundamentals of Programming in VB 1

    21/31

    EVENT PROCEDURE

    WALKTHROUGH When typing code, feature called Member Listing will

    display properties after typing a period after the control

    One proper method highlighted, press Tab key to select

    Line of code where

    typing is occurring

    EVENT PROCEDUREWALKTHROUGH Demonstration code

    Run program

    Type Hello in

    first box

    Typedcharacters

    appear in Blue

  • 7/31/2019 Fundamentals of Programming in VB 1

    22/31

    EVENT PROCEDURE

    WALKTHROUGH

    Press Tab key tochange focus to

    second text box

    changes text to black

    Click on button to

    change text to red

    EVENT PROCEDUREWALKTHROUGH

    Click back to first text box and add text

    Changes color back to blue

  • 7/31/2019 Fundamentals of Programming in VB 1

    23/31

    EVENT PROCEDURE

    WALKTHROUGH

    Adding

    a Quit

    button

    VB.NET EVENTS Name of event procedure can be

    changed since the Handles

    objectName.event identifies the event

    triggering the procedure

  • 7/31/2019 Fundamentals of Programming in VB 1

    24/31

    NUMBERS

    Numbers called numeric literals

    One method of displaying number is with list

    box

    lstBox.Items.Add(n)

    Number n is displayed

    Add is called a method carries out operation and

    displays result

    Another important method is Clear

    lstBox.Items.Clear()

    Erases all items displayed in lstBox

    NUMBERS Example showing different arithmetic

    operations to numbers 3 and 2

  • 7/31/2019 Fundamentals of Programming in VB 1

    25/31

    NUMBERS

    10

    1

    = 10102 = 100

    103 = 1,000

    10n = 10000

    n zeros

    10

    -1

    = 1/10 = 0.110-2 = 0.01

    10-3 = 0.001

    10-n = 0.00001

    n digits

    MM

    Following forms of number all equivalent

    1.4*10^9 1.4E+9 1.4E9 140000000

    3*10^-7 3E-7 0.0000003

    NUMBERS Example use of scientific notation

  • 7/31/2019 Fundamentals of Programming in VB 1

    26/31

    WITH BLOCK

    Reduces repetitive typing and makesprogram less cluttered

    Expression starting with period evaluated asif lstResults.Items preceded it

    NUMBERS Variables quantities identified by name

    Can assign a number to a variable in the

    general form:

    var = n

    Example: if a car travels at 50 miles per hour,

    how far will it travel in 14 hours? Also how

    many hours are required to travel 410 miles?

    Solution uses formula:

    elapsedtime*speeddistance =

  • 7/31/2019 Fundamentals of Programming in VB 1

    27/31

    NUMBERS

    Example solution

    Variables: speed, timeElapsed, distance

    DECLARING A VARIABLE Dim statement declares the type of variable

    Dim varName As Double Declares the variable varName as a double

    precision real number

    Can declare a variable and assign an initialvalue at same time

    Dim varName As Double = 50 Declares varName as double precision and

    assigns it a value of 50

    Can display the current value of a variableand display it in list box using:

    lstBox.Items.Add(varName)

  • 7/31/2019 Fundamentals of Programming in VB 1

    28/31

    NUMBERS

    Example using default value of variableand value of an expression

    BUILT-IN MATH FUNCTIONS VB has some built in math functions that take

    one or more input values and returns a single

    output

    Example:

    Math.Round(2.317,1) is

    2.3

    Int(-2.7) is -

    3

    Math.Sqrt(2) is

    1.414214

    Math.Round(2.317,2) is2.32

    Int(3) is 3.Math.Sqrt(0) is 0.

    Math.Round(2.7) is 3Int(2.7) is 2.Math.Sqrt(9) is 3.

  • 7/31/2019 Fundamentals of Programming in VB 1

    29/31

    BUILT-IN MATH FUNCTIONS

    Example ofbuilt-in

    functions in

    Member listing

    BUILT-IN MATH FUNCTIONS

    When typing, instruction box is

    displayed describing the variables

    needing input

  • 7/31/2019 Fundamentals of Programming in VB 1

    30/31

    NUMBERS

    Example evaluates each function for n

    NUMBERS

    Example evaluates functions at an

    expression

  • 7/31/2019 Fundamentals of Programming in VB 1

    31/31

    NUMBERS

    Integers are declared using thefollowing form

    Dim varName As Integer

    A single Dim statement can be used to

    declare multiple variables

    Dim a, b As Double

    Dim a As Double, b As IntegerDim c As Double = 2, b As Integer = 5

    TYPES OF ERRORS

    Syntax errors

    Due to misspellings, omissions, orincorrect punctuations

    Mostly spotted by editor

    Runtime errors

    Errors occurring while program is running

    Green triangle will appear at left side of lineof code causing the error

    Logical errors

    Program does not perform in way it wasintended