Visual Basic

 

I. Accessing VB from WAM Labs

II. Visual Basic Introduction

    1. Using VB development environment
    2. Form design
    3. Coding
    1. Input / Output
    2. Event processing
    3. Data types
    4. Arrays
    5. Program flow
    6. Using standard controls
    7. File Input / Output

III. Visual Basic projects

    1. Alarm clock (Due Feb 26)
    2. Calendar manager (Due March 12)
    3. Submission

IV. Online Visual Basic Help sites

 

  1. Accessing VB from WAM Labs

 

Windows 3.1

From the Main Menu select Teaching Technologies. At the login prompt enter your Teaching Theater login and password. Teaching Theater Attach window will appear listing a number of application. However, in order to run Visual Basic you need to invoke File Manager application.

Visual Basic application ver 4. is on drive K under \General\VB40.16.

 

Windows NT

Login to the NT machines using your WAM account. Then, click on Network Neighborhood. Select NetWare or Compatible Network and then Theater1or TTClass. It will ask for login and password, use your Teaching Theater login and password. The Visual Basic applications versions 3, 4, and 5 are all in the Apps\General in directories VB, VB40.16, and VB50 respectively.

 

II. Visual Basic Introduction

 

 

  1. Using VB development environment

 

Briefly explain all components:

 

These steps are the first part of the process of creating a Visual Basic application.

    1. Create a new project to organize the parts of your application.
    2. Create a form for each window in your application.
    3. Use the Toolbox to draw the controls for each form.
    4. Create a menu bar for the main form.

 

  1. Form design

 

Describe each GUI object in the Toolbox:

 

Exercise: Design a credit card application form, no coding.

 

  1. Coding

 

Describe code window.

 

The Code window includes:

  1. Input / Output
  2.  

    MsgBox function basically displays a message in a dialog box and waits for the user to choose a button. The MsgBox function returns a value indicating which button the user has chosen.

     

    In the below example it is used to display a simple message:

     

    Sub Form_Load ()

    MsgBox "Hello World!"

     

    End Sub

     

     

    Here in this example, Form_Load function basically executes all the statements on form load. In practice, you make initializations in this function.

     

    Lets work on a more complex example. In this example we are going to use other parameters of the MsgBox function to create more than one button on the message dialog.

     

    Private Sub Form_Load()

    Dim I As Integer

     

    I = MsgBox("Hello World!", vbOKCancel)

     

    End Sub

     

     

    But how can we know which button the user has pressed and take appropriate action ?

     

    Private Sub Form_Load()

    Dim I As Integer

     

    I = MsgBox("Hello World!", vbOKCancel)

     

    If I = vbOK Then

    MsgBox ("OK Pressed")

    End If

    End Sub

     

    In these examples, MsgBox function is used to display only a message. In order to capture user input in the form of text we need to use InputBox function.

     

    Lets work on a simple example.

     

    Private Sub Form_Load()

    Dim T As String

     

    T = InputBox("Your Name?")

     

    MsgBox ("Hi " + T)

    End Sub

     

    Basically in this example a dialog is shown, with a text input box. When either of the buttons are pressed, the input text is captured and displayed in a message box.

     

     

  3. Event processing
  4.  

    Visual basic uses an event-driven architecture. For example, whenever the user clicks one of the mouse buttons, or types in a key on the keyboard, a signal called an event is created. The Windows system then checks event details such as event location, where the mouse click has occurred etc. and then notifies the appropriate widget to take appropriate action.

     

    Let's consider an example. Basically, let's create a simple button called MyButton and write the code that will be executed when the button is clicked. The code below simply changes the caption of the button, when the button is clicked.

     

    Private Sub Mybutton_Click()

    Mybutton.Caption = "Ouchh!"

    End Sub

     

     

    There are a number of event that we can use, for example, double-click, mouse move, mouse up and down events, got focus and lost focus events, key up and down events, drag and drop events, and many others.

     

    Lets work on another example. In this example a text input box (NameIn) is shown and changes in the input box are reflected on a label item (NameOut).

     

    Private Sub NameIn_Change()

    NameOut.Caption = "Your name is " + NameIn.Text

     

    End Sub

     

     

    Let's work on a drag and drop event. In this example, three label items are created with captions as DC, MD, and VA. Dragging and dropping each item on the text item below copies the contents of the label item onto the text item.

     

    Private Sub NameIn1_DragDrop(Source As Control, X As Single, Y As Single)

    NameIn2.Text = Source.Caption

     

    End Sub

     

     

    What could go wrong here ? Simply we are assuming that the dropped object is a label. What if it is a text item would it work ? No simply because the contents a text item is stored in the Text property rather than in the Caption property as it is for the label item. The correct code should check first the type of the dragged item as follows:

     

    Private Sub NameIn2_DragDrop(Source As Control, X As Single, Y As Single)

    If TypeOf Source Is TextBox Then

    NameIn2.Text = Source.Text

    ElseIf TypeOf Source Is Label Then

    NameIn2.Text = Source.Caption

    End If

     

    End Sub

     

     

  5. Data types
  6.  

     

     

    Users can also define compound data types:

     

     

    Type CustRecord

    Name as string

    Address1 as String

    Address2 as String

    City as String

    State as String

    Zip as String

    Balance as Currency

    End Type

     

    Once defined, variables of this type can be declared and used as follows:

     

    Dim ACustomer as CustRecord

     

    ACustomer.Name = "Eser Kandogan"

    ACustomer.Balance = 10000

     

     

    Another data type is the Collection. A collection is basically an ordered set of items that can be referenced as one unit. There are three methods to add, remove, and reference items in a collection, Add, Remove, and Item, respectively. For example,

     

    Dim Employees as New Collection

     

    Employees.Add(item := "Eser")

    Employees.Add(item := "Ben", before := 0)

     

    Employees.Remove 4

     

    MsgBox(Employees(2))

     

    Object Browser is a handy utility for viewing data structures. It can be invoked from the View menu under Object Browser.

     

  7. Arrays
  8.  

    Public Items(19) As Integer 'A global array of 20 integers

    Private Items(49) As String 'A local array of 50 strings

    Private Items(10 To 20) As Integer

    Dim Algebra(1 To 20, 1 To 20) As String 'A 2-D Array of strings

     

    Items(3)

     

    Dynamic Arrays

     

    Dim Items() As String

    ReDim Items(20)

     

    ReDim Preserve Items(Ubound(Items) + 10)

     

    Private Function GetElement(ByRef Items() As Integer)

    X = Items(3)

    End Function

     

  9. Program flow
  10.  

    If statement

     

    If condition Then

    Statements

    Statements

    End If

     

    If condition1 Then

    Statements

    ElseIf condition2 Then

    Statements

    Else

    Statements

    End If

     

    Select statement

     

    Select Case testexpression

    Case expression1

    Statements

    Case expression2

    Statements

    Case Else

    Statements

    End Select

     

    Do While Loop

     

    Do While condition

    Statements

    Loop

     

    Do Until condition

    Statements

    Loop

     

    For Next Loop

     

    For counter=start To end [Step increment]

    Statements

    Next

     

    For Each element In group

    Statements

    Next element

     

     

     

     

     

     

     

    GoTo

     

    GoTo End

    End:

    'I will never use GoTo

     

  11. Using standard controls
  12.  

    ListBox

     

    Private Sub AddButton_Click()

    Call ItemList.AddItem(TextItem.Text)

     

    End Sub

     

    Private Sub DeleteButton_Click()

    Call ItemList.RemoveItem(ItemList.ListIndex)

     

    End Sub

     

     

    What could go wrong here ? In Delete if none of the items is selected, the program will crash. In order to avoid that we need to check the index number.

     

    Private Sub DeleteButton_Click()

    If ItemList.ListIndex <> -1 Then

    Call ItemList.RemoveItem(ItemList.ListIndex)

    End If

     

    End Sub

     

    How can we include replace ?

     

    Private Sub ReplaceButton_Click()

    If ItemList.ListIndex <> -1 Then

    Call ItemList.AddItem(TextItem.Text, ItemList.ListIndex)

    Call ItemList.RemoveItem(ItemList.ListIndex)

    End If

    End Sub

    Timer

     

    Private Sub Timer1_Timer()

    Static A As Integer

     

    A = A Mod 8

    Line1.X1 = A * 100

    Line1.Refresh

     

    A = A + 1

     

    End Sub

     

     

     

  13. File Input / Output

 

Open FileName [For {Append, Binary,Input,Output,Random}] As [#]intfileNumber

 

Close intfileNumber

 

Write #intfileNumber arguments

 

Input #intfileNumber arguments

 

Do Until(Eof(1)

Input #1, Age

Loop

 

 

III. Visual Basic projects

 

  1. Alarm clock (Due Feb 26)
  2.  

    Design only a single mockup dialog for an alarm clock, which allows users to set an alarm by time and days of the week. The design should display the current time and date. Be creative! No coding is required.

     

  3. Calendar manager (Due March 12)

 

A calendar manager is a tool that allows users to keep track of their daily appointments. Design, develop and implement a calendar manager that allows:

1. Insert, delete, and modify appointments

2. Display daily, weekly and monthly views of appointments

3. Report conflicting appointments

4. Provide help on usage

 

Minimum functionality requirements are as follows.

 

Insertion:

Deletion:

Modification:

Display:

 

Conflict reports:

Help:

Implementation note:

Submissions:

Grading criteria:

 

 

Submission

 

 

IV. Online Visual Basic Help sites

 

http://www.microsoft.com/vbasic

http://www.microsoft.com/vbasic/techmat/tutorials/