Guaranteed Hits to your web site!












Learn Visual Basic 6.0


4. More Exploration of the Visual Basic Toolbox


Example 4-1: Temperature Conversion

Start a new project. In this project, we convert temperatures in degrees Fahrenheit (set using a scroll bar) to degrees Celsius. As mentioned in the Review and Preview section, you should try to build this application with minimal reference to the notes. To that end, let's look at the project specifications.


Temperature Conversion Application Specifications


The application should have a scroll bar which adjusts temperature in degrees Fahrenheit from some reasonable minimum to some maximum. As the user changes the scroll bar value, both the Fahrenheit temperature and Celsius temperature (you have to calculate this) in integer format should be displayed. The formula for converting Fahrenheit (F) to Celsius (C) is:

    C = (F - 32)*5/9

To convert this number to a rounded integer, use the Visual Basic CInt() function. To change numeric information to strings for display in label or text boxes, use the Str() or Format() function. Try to build as much of the application as possible before looking at my approach. Try incorporating lines and shapes into your application if you can.


    One Possible Approach to Temperature Conversion Application:

  1. Place a shape, a vertical scroll bar, four labels, and a command button on the form. Put the scroll bar within the shape - since it is in the top-layer of the form, it will lie in the shape. It should resemble this:


    Temperature Conversion



  2. Set the properties of the form and each object:

    Form1: BorderStyle 1-Fixed Single
    Caption Temperature Conversion
    Name frmTemp
    Shape1: BackColor White
    BackStyle 1-Opaque
    FillColor Red
    FillStyle 7-Diagonal Cross
    Shape 4-Rounded Rectangle
    VScroll1: LargeChange 10
    Max -60
    Min 120
    Name vsbTemp
    SmallChange 1
    Value 32
    Label1: Alignment 2-Center
    Caption Fahrenheit
    FontSize 10
    FontStyle Bold
    Label2: Alignment 2-Center
    AutoSize True
    BackColor White
    BorderStyle 1-Fixed Single
    Caption 32
    FontSize 14
    FontStyle Bold
    Name lblTempF
    Label3: Alignment 2-Center
    Caption Celsius
    FontSize 10
    FontStyle Bold
    Label4: Alignment 2-Center
    AutoSize True
    BackColor White
    BorderStyle 1-Fixed Single
    Caption 0
    FontSize 14
    FontStyle Bold
    Name lblTempC
    Command1: Alignment 2-Center
    Cancel True
    Caption E&xit
    Name cmdExit

    Note the temperatures are initialized at 32F and 0C, known values.

    When done, the form should look like this:


    Temperature Conversion



  3. Put this code in the general declarations of your code window.

    Option Explicit
    Dim TempF As Integer
    Dim TempC As Integer


    This makes the two temperature variables global.

  4. Attach the following code to the scroll bar Scroll event.

    Private Sub vsbTemp_Scroll()
      'Read F and convert to C
      TempF = vsbTemp.Value
      lblTempF.Caption = Str(TempF)
      TempC = CInt((TempF - 32) * 5 / 9)
      lblTempC.Caption = Str(TempC)
    End Sub


    This code determines the scroll bar Value as it scrolls, takes that value as Fahrenheit temperature, computes Celsius temperature, and displays both values.

  5. Attach the following code to the scroll bar Change event.

    Private Sub vsbTemp_Change()
      'Read F and convert to C
      TempF = vsbTemp.Value
      lblTempF.Caption = Str(TempF)
      TempC = CInt((TempF - 32) * 5 / 9)
      lblTempC.Caption = Str(TempC)
    End Sub


    Note this code is identical to that used in the Scroll event. This is almost always the case when using scroll bars.

  6. Attach the following code to the cmdExit_Click procedure.

    Private Sub cmdExit_Click()
      End
    End Sub


  7. Give the program a try. Make sure it provides correct information at obvious points. For example, 32 F better always be the same as 0 C! Save the project - we’ll return to it briefly in Class 5.

    Other things to try:


Pen Line


Counter Hit Counter Hit


This Homepage is special brought to you by CK Tan