Temperature Conversion with Multiple Base in VB6

Friday, August 7, 2009 ·

In the earlier posting you have learnt several simple VB programming tips how to convert temperature with basis Celcius, Fahrenheit, Rheamur and Kelvin. In this posting you will learn how to make a simple application to convert temperature with multiple basis with VB6 programming. You can select the temperature basis when the program's running.

The scenario of this simple application is as follow:

  • You can select the temperature basis when the program's running
  • Once you select a temperature basis the conversion process will occur when a value of temperature is not empty (contains a number)
  • Also when you typing a temperature on the temperature text box conversion process will automatically occurs
  • When you typing a number in temperature box, if the basis is empty, it will be automatically set to "Celcius" as default

    Now, how to built your simple application? Let's go on to the simple VB programming tips.
    First, you have to create a screen design likes below image:

    Temperature Conversion Sample Program with multiple basis

    How to create the screen design? Follow step by step of below instructions:
  • Open your VB program, create a project (Project1) and a standard form (Form1)
  • Create 7 label controls
  • Change the label captions into Temperature Conversion, Temperature, Celcius, Fahrenheit, Rheamur, Kelvin, and Basis
  • Create 5 text box controls
  • Clear the text of each text box
  • Rename the text box 1,2,3,4 and 5 into TxtTem, TxtCel, TxtFah, TxtRhe, and TxtKel
  • Create a combo box
  • Clear the combo box text
  • Rename the combo box into cboBasis
    Ok, the screen design already finished. Now, what's the program coding like?

    Here it is the program coding. Follow the next simple VB programming tips.
  • Double-click anywhere in form1 area to create Sub Procedure Form_Load()
  • Write down below program coding in Sub Procedure Form_Load() area

    'to add items in combo box
    cboBasis.AddItem "Celcius"
    cboBasis.AddItem "Fahrenheit"
    cboBasis.AddItem "Rheamur"
    cboBasis.AddItem "Kelvin"


  • Double-click TxtTemp text box to create a Sub TxtTemp_Change()
  • Write down below program coding in the Sub TxtTemp_Change() area

    'call procedure ConvPro
    ConvPro


  • Double-click cboBasis object
  • In the program editor window select "Click" event from drop-down list to create Sub cboBasis_Click()
  • Write down below program code in the Sub cboBasis_Click() area

    'call procedure ConvPro
    ConvPro


  • In the program editor window click General from drop-down list
  • Write down below program code in (General) - (Declarations) area to create a Sub ConvPro()


    Private Sub ConvPro()
    If TxtTemp.Text = "" Then
    'clear the text of text boxes
    TxtCel.Text = ""
    TxtFah.Text = ""
    TxtRhe.Text = ""
    TxtKel.Text = ""
    Exit Sub
    End If

    'if Basis is blank then fill in with "Celcius" as default
    If cboBasis.Text = "" Then
    cboBasis.Text = "Celcius"
    End If
    Select Case cboBasis.Text
    Case "Celcius"
    GoTo ConvCel
    Case "Fahrenheit"
    GoTo ConvFah
    Case "Rheamur"
    GoTo ConvRhe
    Case Else
    GoTo ConvKel
    End Select

    'conversion for celcius basis
    ConvCel:
    TxtCel.Text = Val(TxtTemp.Text)
    TxtFah.Text = Val(TxtTemp.Text) * 1.8 + 32
    TxtRhe.Text = Val(TxtTemp.Text) * 0.8
    TxtKel.Text = Val(TxtTemp.Text) + 273.15
    Exit Sub

    'conversion for fahrenheit basis
    ConvFah:
    TxtFah.Text = Val(TxtTemp.Text)
    TxtCel.Text = (Val(TxtTemp.Text) - 32) / 1.8
    TxtRhe.Text = ((Val(TxtTemp.Text) - 32) / 1.8) * 0.8
    TxtKel.Text = ((Val(TxtTemp.Text) - 32) / 1.8) + 273.15
    Exit Sub

    'conversion for rheamur basis
    ConvRhe:
    TxtRhe.Text = Val(TxtTemp.Text)
    TxtCel.Text = Val(TxtTemp.Text) / 0.8
    TxtFah.Text = ((Val(TxtTemp.Text) / 0.8) * 1.8) + 32
    TxtKel.Text = (Val(TxtTemp.Text) / 0.8) + 273.15
    Exit Sub

    'conversion for kelvin basis
    ConvKel:
    TxtKel.Text = Val(TxtTemp.Text)
    TxtCel.Text = Val(TxtTemp.Text) - 273.15
    TxtRhe.Text = (Val(TxtTemp.Text) - 273.15) * 0.8
    TxtFah.Text = ((Val(TxtTemp.Text) - 273.15) * 1.8) + 32
    End Sub


    So your program will look like below listing:

    Private Sub ConvPro()
    If TxtTemp.Text = "" Then
    'clear text of text boxex
    TxtCel.Text = ""
    TxtFah.Text = ""
    TxtRhe.Text = ""
    TxtKel.Text = ""
    Exit Sub
    End If
    'if Basis is blank then fill in with "Celcius"
    If cboBasis.Text = "" Then
    cboBasis.Text = "Celcius"
    End If
    Select Case cboBasis.Text
    Case "Celcius"
    GoTo ConvCel
    Case "Fahrenheit"
    GoTo ConvFah
    Case "Rheamur"
    GoTo ConvRhe
    Case Else
    GoTo ConvKel
    End Select

    'conversion for celcius basis
    ConvCel:
    TxtCel.Text = Val(TxtTemp.Text)
    TxtFah.Text = Val(TxtTemp.Text) * 1.8 + 32
    TxtRhe.Text = Val(TxtTemp.Text) * 0.8
    TxtKel.Text = Val(TxtTemp.Text) + 273.15
    Exit Sub

    'conversion for fahrenheit basis
    ConvFah:
    TxtFah.Text = Val(TxtTemp.Text)
    TxtCel.Text = (Val(TxtTemp.Text) - 32) / 1.8
    TxtRhe.Text = ((Val(TxtTemp.Text) - 32) / 1.8) * 0.8
    TxtKel.Text = ((Val(TxtTemp.Text) - 32) / 1.8) + 273.15
    Exit Sub

    'conversion for rheamur basis
    ConvRhe:
    TxtRhe.Text = Val(TxtTemp.Text)
    TxtCel.Text = Val(TxtTemp.Text) / 0.8
    TxtFah.Text = ((Val(TxtTemp.Text) / 0.8) * 1.8) + 32
    TxtKel.Text = (Val(TxtTemp.Text) / 0.8) + 273.15
    Exit Sub

    'conversion for kelvin basis
    ConvKel:
    TxtKel.Text = Val(TxtTemp.Text)
    TxtCel.Text = Val(TxtTemp.Text) - 273.15
    TxtRhe.Text = (Val(TxtTemp.Text) - 273.15) * 0.8
    TxtFah.Text = ((Val(TxtTemp.Text) - 273.15) * 1.8) + 32

    End Sub



    Private Sub cboBasis_Click()
    'call procedure ConvPro
    ConvPro
    End Sub


    Private Sub Form_Load()
    'to add items in combo box
    cboBasis.AddItem "Celcius"
    cboBasis.AddItem "Fahrenheit"
    cboBasis.AddItem "Rheamur"
    cboBasis.AddItem "Kelvin"
    End Sub


    Private Sub TxtTemp_Change()
    'call procedure ConvPro
    ConvPro
    End Sub


  • Run your program by clicking Start button on your VB Toolbar
  • Type a number in the text box with label Temperature
  • Select and click different Base from the list on the combo box
    What happen? The conversion should automatically occurs, so the results appear on the each text box. Look at below image for your illustration. Your program should look like it.

    Simple VB Program Temperature Conversion with Multiple Basis


  • 1 comments:

    Temperature Conversion said...
    November 18, 2011 at 1:10 PM  

    Awesome.Thanks for sharing this regarding temperature conversion.This help me lot to done my project.

    Post a Comment