Sunday, August 16, 2009

How to Convert Degree from Fahrenheit to Another in VB6 Programming?

In the previous posting I've told you a simple VB programming tips "How to convert degree from Celcius into another one". In this posting I'll tell you a tips "How to convert degree from Fahrenheit into another one", so the base degree is Fahrenheit. It will be converted into Celcius, Rheamur and Kelvin. As we know that we have three previous conversion formulas (Celcius base):
Fahrenheit = ( Celcius X 1.8 ) + 32
Rheamur = Celcius X 0.8
Kelvin = Celcius + 273.15


From above formulas, we are able to generate three other formulas (Fahrenheit base) as below:
Celcius = ( Fahrenheit - 32 ) / 1.8
Rheamur = ( ( Fahrenheit - 32 ) / 1.8 ) X 0.8
Kelvin = ( ( Fahrenheit - 32 ) / 1.8 ) + 273.15


Now let's implement these formulas in VB6 simple programming. Please follow below steps for your tips!

  • Create a VB project for example Project1
  • Create a VB standard form for example Form1
  • Create 5 labels for example Label1, Label2, Label3, Label4, Label5
  • Go to Label1 properties by clicking Label1 object, and then change Label1 Caption into "Degree Conversion", and change Font Size into 14.
  • Go to Label2 properties, and then change Label2 Caption into "Fahrenheit"
  • Go to Label3 properties, and then change Label3 Caption into "Celcius"
  • Go to Label4 properties, and then change Label4 Caption into "Rheamur"
  • Go to Label5 properties, and then change Label5 Caption into "Kelvin"
  • Create 4 text boxes for example Text1, Text2, Text3, Text4
  • Create a command button for example Command1
  • Go to Command1 properties, and change Command1 caption into "Convert"

    So your design will look like below image (just a simple Screen Design):


    Double click any where in Form1 area to create a Sub Form_Load()
    Write down below program coding in the Sub Form_Load() area

    'to clear the content of text boxes Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = ""


    Double click Command1 command button (with caption "Convert") to create a Sub Command1_Click()
    Write down below program coding in the Sub Command1_Click() area

    'conversion process 'Text1 represents Fahrenheit 'Text2 represents Celcius 'Text3 represents Rheamur 'Text4 represents Kelvin Text2.Text = (Val(Text1.Text) - 32) / 1.8 Text3.Text = ((Val(Text1.Text) - 32) / 1.8) * 0.8 Text4.Text = ((Val(Text1.Text) - 32) / 1.8) + 273.15


    So your program should look like below listing:

    Private Sub Command1_Click() 'conversion process 'Text1 represents Fahrenheit 'Text2 represents Celcius 'Text3 represents Rheamur 'Text4 represents Kelvin Text2.Text = (Val(Text1.Text) - 32) / 1.8 Text3.Text = ((Val(Text1.Text) - 32) / 1.8) * 0.8 Text4.Text = ((Val(Text1.Text) - 32) / 1.8) + 273.15 End Sub


    Private Sub Form_Load() 'to clear the content of text boxes Text1.Text = "" Text2.Text = "" Text3.Text = "" Text4.Text = "" End Sub


    Note: In this simple VB program we haven't used variables yet, but we just use controls from Toolbox. Mybe in the next program we will use variables.

    Ok, your program finish. Test your program by clicking Start button on your VB toolbar to know whether your program's working properly or not.
  • Type a number in Text1 for example 212 (in the text box with label "Fahrenheit")
  • Click command button with label "Convert"
    If the results are 100 in Text2 (Celcius), 80 in Text3 (Rheamur), and 373.15 in Text4 (Kelvin), it means that your program is correct and working properly.
    Look at below image for your illustration. Your program should like it.
    A Simple VB Program Degree Conversion from Fahrenheit

    It's an easy simple VB program isn't it? Would you like to try it?




  • 0 comments:

    Post a Comment