Learn the most popular programming language of the most popular Operating System: Windows

Ad

Writing and Running first Visual Basic Program

Last week you have learned how to run a program that had been made for you.  You probably noticed that when working with Visual Basic, several types of windows could be inspected – even though you were not asked to examine any one window in detail.  Here, you learn about three types of windows:

  • Project window
  • Properties window
  • Code window

You will learn how to move from one window to the next in writing a Visual Basic program.  Also, you will write and run several simple Visual Basic programs.

In working through the next few exercises you will master a number of new tasks, including how to

  • open a new project;
  • save a project;
  • name a form and project;
  • remove a form from a project;
  • add a form to a project;
  • print a form, form text, and code;
  • write a click() procedure;
  • write a KeyPress() procedure;
  • change font size, style and type;
  • change the window state.

Objectives

The tasks for this chapter are distilled into five objectives that teach you the fundamentals of writing and running a Visual Basic program, as follows:

  1. Understanding the concept of a Visual Basic project
  2. Learning how to manage Visual Basic projects
  3. Opening the Properties window and changing form properties
  4. Opening the Code window and writing Visual Basic instructions
  5. Producing a compiled application from a Standard EXE project

Understanding the Concept of a Visual Basic Project

Every Visual Basic application is defined and saved as a project.  Each project, in turn, consists of a collection of files.  These files can include modules, insertable objects, and a single resource file.  Projects themselves are tracked by a project file, which has a .VBP extension.

Form modules, as discussed earlier are first seen as windows with a caption, but Form modules contain coded instructions called procedures for the form itself and for the controls and insertable objects placed on the form.  Other modules include standard modules and class modules.  You will learn the differences among these types of modules as you progress.

The tools of the toolbox represent controls and inserteable objects.  The toolbox can be made visible or invisible by choosing View, Toolbox.  When visible, the toolbox contains a set of icons – each representing a standard or a custom control.  A visual Basic project always includes a set of standard controls and can also include custom controls.  Each custom control has an .OCX extension.  You will be asked to write some beginning Visual Basic procedures for forms.  In “Adding Controls and Event Procedures to Form MOdules, you will add procedures for standard controls.

Projects can also include a resource file, which can contain either binary data, such as images and sounds, or string data.  Resource files need to be created by a Windows resource compiler (typically a C or C++ language compiler), or you can use the resource compiler bundled with Visual Basic.

Share:

Running Visual Basic Applications

Now that you are a bit more familiar with the Visual Basic design environment, let us run few Visual Basic Applications.

Exercise 1.3  Running the Finance Calculator Demo

  1. Create a Folder with the name Finance on your Hard Drive. Copy the Finance Project Source Code, Paste them into Notepad and save them into the Finance folder with the Filename and extension specified:

    Project File: Finance.vbp

    Type=Exe
    Form=Form1.frm
    Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\WINDOWS\system32\stdole2.tlb#OLE Automation
    IconForm="Form1"
    Startup="Form1"
    Command32=""
    Name="Project1"
    HelpContextID="0"
    CompatibleMode="0"
    MajorVer=1
    MinorVer=0
    RevisionVer=0
    AutoIncrementVer=0
    ServerSupportFiles=0
    VersionCompanyName="Home"
    CompilationType=0
    OptimizationType=0
    FavorPentiumPro(tm)=0
    CodeViewDebugInfo=0
    NoAliasing=0
    BoundsCheck=0
    OverflowCheck=0
    FlPointCheck=0
    FDIVCheck=0
    UnroundedFP=0
    StartMode=0
    Unattended=0
    ThreadPerObject=0
    MaxNumberOfThreads=1
    
    [MS Transaction Server]
    AutoRefresh=1
    

    Project File: Finance.vbw

    Form1 = 138, 622, 585, 947, C, 0, 0, 0, 0, 
    

    Project File: Form1.frm

    VERSION 5.00
    Begin VB.Form Form1 
       Caption         =   "New House Payment"
       ClientHeight    =   4665
       ClientLeft      =   5565
       ClientTop       =   2115
       ClientWidth     =   6315
       LinkTopic       =   "Form1"
       ScaleHeight     =   4665
       ScaleWidth      =   6315
       Begin VB.CommandButton cmdCompute 
          Caption         =   "Compute it"
          Height          =   495
          Left            =   1440
          TabIndex        =   3
          Top             =   3840
          Width           =   2175
       End
       Begin VB.TextBox txtYears 
          Height          =   495
          Left            =   3480
          TabIndex        =   2
          Top             =   1800
          Width           =   1815
       End
       Begin VB.TextBox txtRate 
          Height          =   495
          Left            =   3480
          TabIndex        =   1
          Top             =   960
          Width           =   1815
       End
       Begin VB.TextBox txtBorrow 
          Height          =   495
          Left            =   3480
          TabIndex        =   0
          Top             =   240
          Width           =   1815
       End
       Begin VB.Label Label4 
          Caption         =   "Monthly Payments"
          Height          =   495
          Left            =   480
          TabIndex        =   8
          Top             =   2640
          Width           =   2655
       End
       Begin VB.Label Label3 
          Caption         =   "Life of Loan (in years)"
          Height          =   495
          Left            =   480
          TabIndex        =   7
          Top             =   1800
          Width           =   2655
       End
       Begin VB.Label Label2 
          Caption         =   "Yearly interest rate (i.e. 6.5)"
          Height          =   495
          Left            =   480
          TabIndex        =   6
          Top             =   960
          Width           =   2655
       End
       Begin VB.Label Label1 
          Caption         =   "Borrow"
          Height          =   495
          Left            =   480
          TabIndex        =   5
          Top             =   240
          Width           =   2655
       End
       Begin VB.Label lblPayment 
          BorderStyle     =   1  'Fixed Single
          Height          =   495
          Left            =   3480
          TabIndex        =   4
          Top             =   2640
          Width           =   1815
       End
    End
    Attribute VB_Name = "Form1"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Private Sub cmdCompute_Click()
    Dim yearIRate As Single
    Dim nperiod As Integer
    Dim borrow As Currency
    Dim payment As Currency
    
    yearIRate = CSng(txtRate.Text) / (12 * 100)
    nperiod = CInt(txtYears.Text) * 12
    borrow = CCur(txtBorrow.Text)
    
    payment = Pmt(yearIRate, nperiod, -borrow, 0, 0)
    lblPayment.Caption = Format(payment, "Currency")
    
    End Sub
    
  2. Start Visual Basic from Windows.
  3. Select Open Project from File Menu. If a dialog box appears asking you to save a file, click No.
  4. Browse to the Finance folder, where you have saved Finance Project files and select Finance.vbp File and click the Open button.
  5. Choose Run, Start from the Visual Basic menu, or press F5. This runs the Visual Basic application.
  6. Enter Value 100,000 into the Text Control with the label borrow
  7. Enter Value 7.5 into the Text control with the label Yearly Interest Rate.
  8. Enter Value 30 into the Text control with the label Life of loan.
  9. Click on the Command Button to Calculate and display the Monthly Payments.
  10. Choose Options, Exit to exit the application and return to the Visual Basic design environment.
  11. Choose File, Remove Project to remove the Project from the design environment.

Exercise 1.4  Running the Highest and Lowest Score Values Demo

  1. Create a Folder with the name Score on your Hard Drive. Copy the Score Project Source Code, Paste them into Notepad and save them into the Score folder with the Filename and extension specified:

    Project File: HighLow.vbp

    Type=Exe
    Form=Form1.frm
    Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\WINDOWS\system32\stdole2.tlb#OLE Automation
    IconForm="Form1"
    Startup="Form1"
    Command32=""
    Name="Project1"
    HelpContextID="0"
    CompatibleMode="0"
    MajorVer=1
    MinorVer=0
    RevisionVer=0
    AutoIncrementVer=0
    ServerSupportFiles=0
    VersionCompanyName="Home"
    CompilationType=0
    OptimizationType=0
    FavorPentiumPro(tm)=0
    CodeViewDebugInfo=0
    NoAliasing=0
    BoundsCheck=0
    OverflowCheck=0
    FlPointCheck=0
    FDIVCheck=0
    UnroundedFP=0
    StartMode=0
    Unattended=0
    ThreadPerObject=0
    MaxNumberOfThreads=1
    
    [MS Transaction Server]
    AutoRefresh=1
    
    

    Project File: HighLow.vbw

    Form1 = 648, 202, 1253, 947, I, 0, 0, 0, 0, 
    

    Project File: Form1.frm

    VERSION 5.00
    Begin VB.Form Form1 
       Caption         =   "Get Score"
       ClientHeight    =   3765
       ClientLeft      =   5385
       ClientTop       =   2310
       ClientWidth     =   7785
       LinkTopic       =   "Form1"
       ScaleHeight     =   3765
       ScaleWidth      =   7785
       Begin VB.TextBox txtScore 
          Height          =   495
          Index           =   1
          Left            =   1080
          TabIndex        =   0
          Top             =   240
          Width           =   1215
       End
       Begin VB.TextBox txtScore 
          Height          =   495
          Index           =   2
          Left            =   2400
          TabIndex        =   1
          Top             =   240
          Width           =   1215
       End
       Begin VB.TextBox txtScore 
          Height          =   495
          Index           =   3
          Left            =   3720
          TabIndex        =   2
          Top             =   240
          Width           =   1215
       End
       Begin VB.TextBox txtScore 
          Height          =   495
          Index           =   4
          Left            =   5040
          TabIndex        =   3
          Top             =   240
          Width           =   1215
       End
       Begin VB.TextBox txtScore 
          Height          =   495
          Index           =   5
          Left            =   6360
          TabIndex        =   4
          Top             =   240
          Width           =   1215
       End
       Begin VB.TextBox txtScore 
          Height          =   495
          Index           =   14
          Left            =   5040
          TabIndex        =   13
          Top             =   1440
          Width           =   1215
       End
       Begin VB.TextBox txtScore 
          Height          =   495
          Index           =   13
          Left            =   3720
          TabIndex        =   12
          Top             =   1440
          Width           =   1215
       End
       Begin VB.TextBox txtScore 
          Height          =   495
          Index           =   12
          Left            =   2400
          TabIndex        =   11
          Top             =   1440
          Width           =   1215
       End
       Begin VB.TextBox txtScore 
          Height          =   495
          Index           =   11
          Left            =   1080
          TabIndex        =   10
          Top             =   1440
          Width           =   1215
       End
       Begin VB.TextBox txtScore 
          Height          =   495
          Index           =   10
          Left            =   6360
          TabIndex        =   9
          Top             =   840
          Width           =   1215
       End
       Begin VB.TextBox txtScore 
          Height          =   495
          Index           =   9
          Left            =   5040
          TabIndex        =   8
          Top             =   840
          Width           =   1215
       End
       Begin VB.TextBox txtScore 
          Height          =   495
          Index           =   8
          Left            =   3720
          TabIndex        =   7
          Top             =   840
          Width           =   1215
       End
       Begin VB.TextBox txtScore 
          Height          =   495
          Index           =   7
          Left            =   2400
          TabIndex        =   6
          Top             =   840
          Width           =   1215
       End
       Begin VB.TextBox txtScore 
          Height          =   495
          Index           =   6
          Left            =   1080
          TabIndex        =   5
          Top             =   840
          Width           =   1215
       End
       Begin VB.CommandButton cmdScores 
          Caption         =   "Set High and Low Scores"
          Height          =   495
          Left            =   600
          TabIndex        =   15
          Top             =   2880
          Width           =   2655
       End
       Begin VB.TextBox txtScore 
          Height          =   495
          Index           =   0
          Left            =   6360
          TabIndex        =   14
          Top             =   1440
          Width           =   1215
       End
       Begin VB.Label Label5 
          Caption         =   "Team 3"
          Height          =   495
          Left            =   120
          TabIndex        =   22
          Top             =   1440
          Width           =   855
       End
       Begin VB.Label Label4 
          Caption         =   "Team 1"
          Height          =   495
          Left            =   120
          TabIndex        =   21
          Top             =   240
          Width           =   855
       End
       Begin VB.Label Label3 
          Caption         =   "Team 2"
          Height          =   495
          Left            =   120
          TabIndex        =   20
          Top             =   840
          Width           =   855
       End
       Begin VB.Label lblLow 
          BorderStyle     =   1  'Fixed Single
          Height          =   495
          Left            =   5640
          TabIndex        =   19
          Top             =   2880
          Width           =   1935
       End
       Begin VB.Label lblHigh 
          BorderStyle     =   1  'Fixed Single
          Height          =   495
          Left            =   5640
          TabIndex        =   18
          Top             =   2280
          Width           =   1935
       End
       Begin VB.Label Label2 
          Caption         =   "Low Score is"
          Height          =   495
          Left            =   3600
          TabIndex        =   17
          Top             =   3000
          Width           =   1815
       End
       Begin VB.Label Label1 
          Caption         =   "High Score is"
          Height          =   495
          Left            =   3600
          TabIndex        =   16
          Top             =   2400
          Width           =   1815
       End
    End
    Attribute VB_Name = "Form1"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Private Sub cmdScores_Click()
    Static score(0 To 14) As Integer
    Dim num As Integer
    Dim High As Integer
    Dim Low As Integer
    For num = 0 To 14
      score(num) = Val(txtScore(num).Text)
    Next num
       High = High_Score(score(), num - 1)
       lblHigh.Caption = Str$(High)
       Low = Low_Score(score(), num - 1)
       lblLow.Caption = Str$(Low)
    End Sub
    
    Private Function High_Score(score() As Integer, num As Integer)
    Dim max As Integer
    Dim High As Integer
    High = 0
    For max = 0 To num
       If (score(max) > High) Then
           High = score(max)
       End If
    Next max
    High_Score = High
      
    End Function
    
    Private Function Low_Score(score() As Integer, num As Integer)
    Dim min As Integer
    Dim Low As Integer
    Low = 1000
    For min = 0 To num
       If (score(min) < Low) Then
           Low = score(min)
       End If
    Next min
    Low_Score = Low
      
    End Function
    
    
  2. Start Visual Basic from Windows.
  3. Select Open Project from File Menu. If a dialog box appears asking you to save a file, click No.
  4. Browse to the Score folder, where you have saved the Score Project files, and select HighLow.vbp File and click the Open button.
  5. Choose Run, Start from the Visual Basic menu, or press F5. This runs the Visual Basic application.
  6. Enter some random numbers into the 15 Text Boxes given with the labels Team 1, Team 2 and Team 3.
  7. Click on the Command Button to find the Highest and Lowest score values and display them.
  8. Choose Options, Exit to exit the application and return to the Visual Basic design environment.
  9. Choose File, Remove Project to remove the Project from the design environment.

Exercise 1.5  Running the Race Pace Demo

  1. Create a Folder with the name Race on your Hard Drive. Copy the Race Project Source Code, Paste them into Notepad and save them into the Score folder with the Filename and extension specified:

    Project File: Race.vbp

    Type=Exe
    Form=..\Highlow\Race\Form1.frm
    Reference=*\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\WINDOWS\system32\stdole2.tlb#OLE Automation
    Startup="Form1"
    Command32=""
    Name="Project1"
    HelpContextID="0"
    CompatibleMode="0"
    MajorVer=1
    MinorVer=0
    RevisionVer=0
    AutoIncrementVer=0
    ServerSupportFiles=0
    VersionCompanyName="Home"
    CompilationType=0
    OptimizationType=0
    FavorPentiumPro(tm)=0
    CodeViewDebugInfo=0
    NoAliasing=0
    BoundsCheck=0
    OverflowCheck=0
    FlPointCheck=0
    FDIVCheck=0
    UnroundedFP=0
    StartMode=0
    Unattended=0
    ThreadPerObject=0
    MaxNumberOfThreads=1
    
    [MS Transaction Server]
    AutoRefresh=1
    
    

    Project File: Race.vbw

    Form1 = 136, 117, 583, 940, , 0, 0, 0, 0, 
    

    Project File: Form1.frm

    VERSION 5.00
    Begin VB.Form Form1 
       Caption         =   "Convert Race Result"
       ClientHeight    =   4155
       ClientLeft      =   2040
       ClientTop       =   1890
       ClientWidth     =   6585
       LinkTopic       =   "Form1"
       ScaleHeight     =   4155
       ScaleWidth      =   6585
       Begin VB.CommandButton cmdClear 
          Caption         =   "Clear"
          Height          =   495
          Left            =   840
          TabIndex        =   7
          Top             =   3240
          Width           =   1575
       End
       Begin VB.TextBox txtTime 
          Height          =   495
          Left            =   4680
          TabIndex        =   3
          Top             =   720
          Width           =   1575
       End
       Begin VB.Frame Frame1 
          Caption         =   "Click on one"
          Height          =   2175
          Left            =   480
          TabIndex        =   0
          Top             =   600
          Width           =   2415
          Begin VB.OptionButton Opt8K 
             Caption         =   "8 K Run/Walk"
             Height          =   255
             Left            =   360
             TabIndex        =   2
             Top             =   1440
             Width           =   1815
          End
          Begin VB.OptionButton Opt10K 
             Caption         =   "10 K Run/Walk"
             Height          =   255
             Left            =   360
             TabIndex        =   1
             Top             =   720
             Width           =   1815
          End
       End
       Begin VB.Label Label3 
          Caption         =   "Pace [in minutes per mile]"
          Height          =   855
          Left            =   3000
          TabIndex        =   6
          Top             =   1800
          Width           =   1335
       End
       Begin VB.Label lblPace 
          Height          =   495
          Left            =   4680
          TabIndex        =   5
          Top             =   1920
          Width           =   1575
       End
       Begin VB.Label Label1 
          Caption         =   "Enter Time in Minutes [i.e. 50.5] and press Enter"
          Height          =   855
          Left            =   3000
          TabIndex        =   4
          Top             =   720
          Width           =   1335
       End
    End
    Attribute VB_Name = "Form1"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    
    
    Private Sub cmdClear_Click()
       lblPace.Caption = ""
       txtTime.Text = ""
       txtTime.SetFocus
    End Sub
    
    Private Sub txtTime_KeyPress(KeyAscii As Integer)
    Dim result As Single
    Dim newtime As Single
    
       If KeyAscii = vbKeyReturn Then
          newtime = CSng(txtTime.Text)
          result = mph(newtime)
          lblPace.Caption = Str$(result)
       Else
          Exit Sub
       End If
    
    End Sub
    
    
    Function mph(newtime As Single) As Single
       If (Opt10K = True And newtime > 0) Then
          mph = CSng(newtime / 6.2)
       ElseIf (Opt8K = True And newtime > 0) Then
          mph = CSng(newtime / 5.1)
       End If
       txtTime.SetFocus
    
    End Function
    
  2. Start Visual Basic from Windows.
  3. Select Open Project from File Menu. If a dialog box appears asking you to save a file, click No.
  4. Browse to the Race folder, where you have saved the Race Project Files and select Race.vbp File and click Open button.
  5. Choose Run, Start from the Visual Basic menu, or press F5. This runs the Visual Basic application.
  6. Click on one of the Option Buttons to select it.
  7. Enter the value 48.45 into the Text Box.
  8. Press Enter key to calculate the pace of race in miles required per minute and display it in the label below the Text Box.
  9. Click on the Command Button to clear the values and try it on a different set of values
  10. Choose Options, Exit to exit the application and return to the Visual Basic design environment.
  11. Choose File, Remove Project to remove the Project from the design environment.

We will learn how to design these Forms and programs at a later stage.

Technorati Tags: ,,
Share:

Running Sample Visual Basic Programs

Given this brief introduction to Visual Basic, it is time for you to become more familiar with the Visual Basic programming environment.  Still, you are not expected to write a Visual Basic program at this time; right now, you’ll start Visual Basic and become a bit more familiar with the way in which the environment can be configured.

A Quick Tour of the Visual Basic Design Environment.

The Visual Basic design environment is almost infinitely configurable.  This makes it difficult to describe a standard Visual Basic user interface.  It depends upon the way the design environment was last configured.

There are two overall design environment choices: MDI or SDI.  The abbreviation MDI stands for multiple-document interface, and it is the default Visual Basic setting.  It features an overall container window and movable and dockable windows inside the container.  It is the environment shown under the topic Low-Visual and High-Visual Languages.  The abbreviation SDI stands for single-document interface.  It features free-floating windows on the Windows desktop, which is the environment that would be most familiar to you if you have worked with previous versions of Visual Basic.

Whether the environment is MDI or SDI, the Visual Basic start-up environment frequently displays the windows and utilities in the Table 1.1 below.  These windows and utilities are described in detail later.

Table 1.1  Visual Basic Startup Windows
Project Explorer window This window is frequently positioned on the right of the Visual Basic container. It lists the modules that make up a Visual Basic project
Form Module window This Window displays a form if a form is selected for viewing in the Project Explorer. This form is selected for viewing in the Project Explorer. This form is the "canvas" upon which controls are placed.
Properties window This window displays the properties for whatever object has focus - whether a module or control.
Toolbox window This window displays controls for placing on forms.

The MDI Design Environment: Using Dockable Windows

The multiple-document interface for Visual Basic 5.0 is new.  It allows you to configure the Visual Basic design environment in many ways, but it can be confusing.  In order to use it effectively, you must have a large screen set to a fairly high resolution; otherwise, you will quickly suffer from screen clutter.  It also requires that you know how to work with dockable windows, an interface feature designed for those who prefer to use a mouse, rather than the keyboard, as a principal input device.

If you have not used dockable windows inside a container before, you might find them frustrating at first.  Dockable windows allow you to take any window inside a container and dock it on the perimeter of the container.  As you move a window inside a container by keeping your mouse button depressed on the window title bar while moving it, an outline of the window is displayed.  The width of this band gives you a visual cue as to what you can do with the window.  A thick band outline indicates it will become free-floating if released by the mouse button.  Figure 1.8  shows the toolbox docked to the right-hand side of a form.

Figure 1.8 The toolbox will be docked at the side of the Visual Basic form when the Dockable command (right-mouse button) is selected.

Given the capability to dock any window at any position in the Visual Basic MDI container, it is possible to customize the environment to your liking.  Many developers prefer to dock the toolbox at the bottom of the container in order to make room for form design.

To view a window or toolbar that is not displayed, choose View from the Visual Basic menu to see a list of windows and design-time utilities.

The capability to dock windows within the MDI environment can be controlled through the Docking tab of the Options dialog box (choose Tools, Options).  If the docking option for a window is not selected (see Figure 1.9), it will remain free floating.

Figure 1.9 The Docking tab of the Options dialog box presents many possibilities for docking the Visual Basic windows and design tools, such as the Color palace.

The SDI Design Environment: Using Free-Floating Windows

The single-document interface, the one preferred by the authors, essentially makes all windows free-floating outside of any container.  This increases the amount of screen “real estate” available, which is important in the rich (and often times cluttered) design environment of Visual Basic.  The SDI design environment is probably also more familiar to you from working other Windows applications, including earlier versions of Visual Basic.

To move to an SDI environment, select the Advanced tab in the Options dialog box (by choosing Tools, Options), as shown in the image below.

Figure 1.10  The SDI selection in the Options dialog box creates a design environment in which windows remain separate and are no longer docked.

Once the SDI environment is selected, a dialog box will appear, informing you that your selection will take effect the next time Visual Basic is launched.  To view the SDI environment, exit Visual Basic (by choosing File, Exit), and launch Visual Basic again.  You will now be in an SDI environment similar to the one you see in Figure 1.11.  To view individual windows and toolbars, you can make selections under the Visual Basic View menu.

Figure 1.11  The Visual Basic single-document interface (SDI) features free-floating windows, and is good to use when you work on a small screen.

Running Visual Basic Applications

Now that you are a bit more familiar with the Visual Basic design environment, let’s run several Visual Basic applications.

Share:

Software Components

After all of this discussion of classes and objects, you might be asking yourself, why all the fuss about objects?  What are the advantages of using objects?

The real benefit of objects is that, developed properly, they provide software that is reusable and robust.  The continuing dilemma of software development has been to create reliable code much faster and at lower cost.  Some claim that it is an impossible task.  More than a few software developers have a large sign posted above their doors: “Software Developed: Fast, Cheap, Good. Choose Two.”

Although object-oriented design has certainly not proved to be a magic bullet to shoot the beast of software development costs, it has helped create reliable, reusable software components.  Visual Basic components include the following:

  • Visible controls (the tools represented in the Visual Basic toolbox)
  • Software libraries that can be reliably used by many applications (ActiveX DLLs)
  • Executable programs that can be used by other programs (ActiveX EXEs)

These components make single or multiple objects available to the developer.

Different types software components are introduced and used here.  You will build applications using software components, and in the case of ActiveX DLLs, build a software component yourself.  Software components make possible rapid development of applications by acting as the building blocks of an application.  Each software component provides a particular service to the application.  One way to think about software components is to see them as servers to client applications.

How do you know which software components are available to you?  Visual Basic provides the Object Browser utility to let you examine your choices.

Launching Visual Basic and the Object Browser

In this exercise, you will start Visual Basic and then use Object Browser to examine Visual Basic objects.  You are provided step-by-step instructions.  Don’t be alarmed if you see unfamiliar windows.  The Visual Basic environment is explained in greater detail later in “Writing and Running Your First Visual Basic Program”, and “Adding Controls and Event Procedures to Form Modules.”  This exercise assumes Visual Basic has been installed on your computer.

  1. Start Visual Basic from the Windows Start Menu.
  2. When Visual Basic launches, a dialog box similar to that shown below appears.  If this dialog box does not appear, select Project, New from the Visual basic menu.

    The Visual Basic startup dialog box contains several types of new projects, including the Standard EXE project.

  3. Click the Open button for a new Standard EXE project (or click OK if you opened the dialog box from the Visual Basic menu). The Visual Basic design environment will appear. It should look somewhat similar to the design environment you see illustrated earlier. It will appear similar, but not identical. As you will discover in the next section, the Visual Basic design environment is almost infinitely configurable by you. There really is no standard Visual Basic design environment.
  4. Press the F2 key or select View, Object Browser from the Visual Basic Menu. Object Browser will appear as shown below.
  5. Select the VB library from the combo box in the upper-left corner of Object Browser, as shown below (You have used this kind of Windows object before, but you may not have known that was called a combo box.)

    The VB Object Browser lists the software classes available to you from the Visual Basic class library

  6. The classes through which you can scroll are listed on the left-hand side of Object Browser. Select the TextBox class of the VB Library and then examine the properties, methods, and events of the TextBox class in the right-hand list box.

Notice that the Object Browser icons indicate whether the class element (called a class member) is a property (the icon depicting a small hand holding a piece of paper), a method (the icon depicting a small green rectangle), or an event (the icon depicting a small thunderbolt).  The image below shows these three icons.

Object Browser displays information about classes, in this case the TextBox classes, and shows properties of the class, class events, and class methods.

As you examine the TextBox class and select different properties, methods, and events, observe the bottom of Object Browser where the class member is described.

Technorati Tags: ,,
Share:

Trending

Popular Posts

Search This Blog

Powered by Blogger.

Featured Post

Coded Statements and Methods

In the preceding procedure , you wrote several coded instructions, which introduced two different programming statements and a method: the D...

Recent Posts