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

Ad

Showing posts with label Running Programs. Show all posts
Showing posts with label Running Programs. Show all posts

Coding Event Procedure for Control

Coding an Event Procedure for a Control

Adding a control to a form and setting the control properties set the scene for adding a procedure.  Remember that a control will do nothing to an event-oriented environment unless an event procedure is attached to it.  To add to this concept, more than one event can be associated with a single control, as with a form.  In the next exercise you will examine the various event and alternatives for a single control.

Controlling Naming Conventions

When adding an event procedure, a default name is always assigned to the form or control unless you decide to change it.  For example, the first command button added to a form is named Command1, and the second is Command2. In this text, we ask you to follow a command syntax by adding a name prefix to a control (and many times to forms).  For example, if you decide to add a command button to a form and change the command caption to Start, you would name the control cmdStart.  The cmd is the command prefix.  The prefix always tells you what specific control you are working with.

The exception to the above rule is when you are working with labels that have no effect on the input or output to a program.  Because changing the name of each control takes considerable time, you should often leave label conventions (Label1, Label2, and so on) alone, unless the content of a label is determined by a procedure.  This separates labels whose content is determined by a procedure from those whose content is not determined by a procedure.

Table 3.1 shows the controls, the default names and the name prefixes that are used for the 12 controls.

Table 3.1 The 12 Controls
ControlDefault NameName Prefix
Text BoxText1txt
LabelLabel1 txt
List BoxList1lst
Combo Boxcombo1cbo
Picture Boxpicture1pic
FrameFrame1fra
Option Buttonoption1opt
Command Buttoncommand1cmd
Check Boxcheck1chk
Image Image1img
Horizontal Scroll BarHScroll1hsb
Vertical Scroll BarVScroll1vsb

Exercise 3.2 Adding a Procedure to a Command Button

For this exercise, you write a procedure and attach it to a command button. Figure 3.4 shows how the form will look when you run the program.

Figure 3.4

In this display, the user is expected to click the Start button to display the message on the form.

  1. Begin a new project or continue the last.
  2. Add a command button to the form.
  3. With the form active (not the command button), press F4 to reveal the properties box.  Make the following change:

    Caption = A Printed Message

  4. With the command button active, press F4 to reveal the properties box. Make the following changes:

    Caption = Start

    Name = cmdStart

  5. With the command button still active, double-click to open the Code window.  Enter the following code, between the Private Sub cmdStart_Click() and the End Sub instructions:
    Private Sub cmdStart_Click()
    'The Start command button is used to display a message
    Dim message As String
    Print
    message$ = "Welcome to Visual Basic" Print Spc(20); message$ ' prints 20 spaces followed by the message End Sub
  6. Save the project.
  7. Close the Code window and run the program.  Click the Start button.
  8. Exit and open the Code window to examine the other types of events that can be assigned to the cmdStart control.  For example, instead of writing a cmdClick() procedure, you might write a MouseDown() procedure, such that when the user presses the mouse button, the message is displayed.  As another alternative, write a MouseUp() procedure.
Share:

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 a windows with a caption, but little else.  From 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 insertable 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.  In this section you are asked to write some beginning Visual Baisc procedures for forms.  In “Adding Controls and Event Procedures to Form Modules,” you will add procedures for stand 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.

The Files that Make Up a Stanbdard EXE Project

In working with Visual Basic, you are asked to work with different types of files.  Each file type is given a unique file extension, which enables you to identify it.  Visual Basic contains over two dozen types of files, each of which has a different extension.  The Table below shows eight common file types contained within a project, their extensions, and their meanings.

Table 2.1 Common File Types
Type of File Extension Extension
Standard Module .BAS Each coded module is saved as a file
Class Module .CLS Each class module is saved as a file
Form Module .FRM Each form is saved as a file
Binary .FRX Each Icon or Picture property value is saved by Visual Basic in .FRX form
Log .LOG A file for logging load errors.
Resource .RES A project can have a single resource file for storing binary and string data
Project .VBP Each project is saved as a file.
Workspace .VBW Each workspace for a project is saved as a file.

This file notation is used throughout.  When you save a form, it is saved with a .FRM extension, and all event procedures written for that form are also saved in the .FRM file.  Because procedures are not saved as separate files with their own unique extension, they become bound to forms.  When you write and save a coded standard module, it is saved with a .BAS extension.  Coded modules are thus different from event procedures written for forms.  Coded modules are not bound to forms.

The Project Window (Project Explorer) in the Visual Basic Design Environment

Whenever you begin a Visual Basic program, you begin a new project.  Each project can contain several forms, several standard and class coded modules, and custom controls.  The Project window displays a list of all modules assigned to a project.  Whenever you begin a new project, a default Project window is created (see Fiigure 2.1) .  If the window fails to appear or is hidden, choose View, Project Explorer; press Ctrl+R; or click the Project Explorer icon.  In addition, your environment can be set to remove the docking of the Project Explorer.  This setting is controlled by choosing Tools, Options, clicking the Docking tab, and turning off the Project Explorer and Object browser selections.

Figure 2.1: This Project window reveals that Project1 contains a forms folder that is limited to a single form.

Figure 2.1 illustrates a Project window that contains three small icons on the left-hand side.  The leftmost icon enables you to view code, while the middle one enables you to view the underlying object (which in this case is the form).  Forms, the third icon, is important when projects become large and contain several forms or when projects are combined.  With large projects, all forms are placed in one folder, and all modules are placed in a second.  Toggling forms enables you to show or hide all forms for a project that are contained in the Forms folder.

Share:

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:

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...

Labels

Recent Posts