Celsius to Fahrenheit Converter in Visual Basic

Problem:

Write a program in visual basic 6 that will convert degree Celsius to degree Fahrenheit and vice versa.

Tutorial Description:

We will create program that will allow the user to enter a value in Celsius or Fahrenheit and converts it to its corresponding value (ex. If user enters in Celsius field then the program will convert it to Fahrenheit and same goes the other way. )

Let’s begin the tutorial

  1. Open your visual basic 6 program and select Standard EXE.
  2. In this program we need the following controls:
    1. 3 labels
    2. 2 textboxes
    3. 2 command buttons
  3. Sample layout or design of the form.

celsius1

Change the name of the first textbox (Degree Celsius textbox) the one with the 0 value on the text into txtc.

The second textbox (Degree Fahrenheit) the one with the 32 value into txtf.

  1. Next, double click the txtc (the first textbox) and paste the code.
If txtc.Text = "" Or IsNumeric(txtc.Text) = False Then
MsgBox "Please enter a numeric value"
txtc.SetFocus
Else
txtf.Text = Format(Val(txtc.Text) * (9 / 5) + 32, "##,##0.00")
End If

It must proceed to the change event of the textbox.

Private Sub txtc_Change()
'paste the code here
End Sub

Code Explanation:

The code will validate first the input of the user, an error message will appear if the user enters a non-numeric value. The program will then convert the input (degree Celsius) into degree Fahrenheit.

Equation:

F = C * (9 / 5) + 32

Where:

F is the Fahrenheit

C is the given Celsius (input of the user)

  1. We will now do the reverse version (Fahrenheit to Celsius). Double click txtf (the second textbox) and paste the code below.
If txtf.Text = "" Or IsNumeric(txtf.Text) = False Then
MsgBox "Please enter a numeric value"
txtf.SetFocus
Else
txtc.Text = Format((Val(txtf.Text) - 32) * (5 / 9), "##,##0.00")
End If

Code Explanation:

The explanation for the code is same with the previous one, except this time it will convert Fahrenheit to Celsius.

Equation:

C = (F -32) * (5 / 9)

  1. To close or end the program, double click the Close button and type End
  2. To refresh, paste the code.
txtc.Text = 0
txtf.Text = 32
  1. Save the project and press F5 to test and run the project.

Download

, ,

Post navigation