Tuesday, November 13, 2007

Visual Basic

What is Programming?
The process of writing or creating list of instruction to be carried out by the computer.Programmer writes programs on a computer.

Programming Language

  • There are hundreds of programming languages:
    • Procedural language -> BASIC, C, COBOL, FORTAN
    • Object-oriented event-driven programming language(OOED)
  • So, Visual Basics falls into which category?

Is an OOED language.
Why?
1. Was developed to help create programs that will work with the Windows Operating System.
2. Object-oriented language - form, button, image
3. Event driven: does not follow a predefined sequence of instruction; it responds to events(click, load, mouse down) to execute different sets of instructions.

Introduction to Visual Basic



Writing Windows Applications with Visual Basic
  • VB runs in the Microsoft Windows environment
  • VB provides the tools to create windows with familiar elements
  • Microsoft Windows uses a graphical user interface (GUI) – forms, controls -> Event-driven Programming


  • Object Model
    •In VB, we will work with objects, properties and methods:
    Objects: Thing/Noun - Forms, controls
    Properties: Tell something about an object – name, color, size, location, behavior/Adjective
    [Object.Property] -> Form1.Caption
    Methods: Actions associated with objects/Verb – Move, Print, Resize, Clear
    [Object.Method]-> Form1.Print
    •Event – clicking on a command button, moving the mouse, resizing the window

    Visual Basic Projects
    •VB project consists of at least two and more files:
    –.vbp (project file) – a small text file that holds the names of the other files in the project and some information about the VB environment.
    –.frm – holds a description of all objects and their properties for the form and the Basic code written to respond to the events.
    –.bas (standard code modules)– holds Basic statements that can be accessed from any form
    –.ocx – include controls that are not part of the standard control set.vbw – holds information about each of the project’s forms.

    Visual Basic Event Procedure
    •Code is written in procedures
    •Two types of procedures: Sub & Function
    •Sub procedure begins with Private Sub and end with End Sub
    •VB automatically names the event procedures; consists of the object name, an underscore, and the name of the event.
    Example: the Click event for a command button called cmdCalculate -> cmdCalculate_Click

    An Event Procedure Walkthrough
    •Create the interface.
    •Set Properties.
    •Double click on the object to open the Code window.
    •Click on the Procedure box to find the event
    •Write the code for that event.

    Example of An Event
    Private Sub objectName_event ( )
    statements
    End Sub
    Private Sub txtOne_GotFocus( )
    txtOne.Font.Size = 12
    txtOne.Font.Bold = False
    End Sub


    More Example
    Private Sub cmdButton_Click( )
    txtBox.ForeColor = vbRed
    txtBox.Font.Size = 24
    txtBox.Text = “Hello”
    End Sub


    Visual Basic Code Statement
    •Remark statement/Comments
    –Used for project documentation only.
    –Not considered executable and have no effect when the project runs
    –Purpose: To make the project more readable and understandable
    –Begin with an apostrophe
    –Example: ‘This project was written by Alicia


    Visual Basic Code Statement
  • End statement

  • –Stops execution of a project
    –Example: End


    InputBox Function
  • Used to request input from the user

  • In the input box, we can display a message called prompt and allow the user to type input into the text box

  • General Form:
  • VariableName = InputBox(“Prompt”[, “Title”] [Default] [, XPos] [, YPos])
  • Example:
  • strName = InputBox("What is your name ?", "Input Request")
  • The Prompt must be enclosed in quotation marks

  • The Title displays in the title bar. If the Title is missing, the project name appears in the title bar

  • Any value we place in Default appears in the text box when it is displayed; otherwise the text box is empty

  • XPos and YPos define the measurement in twips for the left and top edge of the box





  • Message Box
  • A message box displays a message to the user

  • We can display a message, an optional icon, a title bar caption and a command button(s) in a message box

  • General Form:

  • MsgBox “Message string” [, Buttons/icon] [, “Caption of title bar”]
  • Example:

  • MsgBox “Please Enter your name.”, vbOKOnly, “Name Missing”
  • The Message string is the message appeared in the message box

  • The Buttons is optional – it determines the command buttons that will display and any icons that will appear

  • If we omit the Caption of title bar, the project’s name will appear in the message box title bar



  • 0 comments: