Resources » Code Snippets » Winforms Controls

Celsius to Farenheit Conversion and vice versa


Posted Date: 27-Jan-2008  Last Updated:   Category: Winforms Controls    
Author: Member Level: Bronze    Points: 7



The code sample, written in VB.NET, is a Windows Forms application that converts temperature between two units of measurement - celsius and farenheit.

The form contains various controls - textboxes and buttons - for input. The code for conversion is pretty simple, though.


Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents lblRed As System.Windows.Forms.Label
Friend WithEvents lblBlue As System.Windows.Forms.Label
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents hs As System.Windows.Forms.HScrollBar
Friend WithEvents f1 As System.Windows.Forms.Label
Friend WithEvents c1 As System.Windows.Forms.Label
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents TrackBar1 As System.Windows.Forms.TrackBar
Private Sub InitializeComponent()
Me.hs = New System.Windows.Forms.HScrollBar()
Me.lblRed = New System.Windows.Forms.Label()
Me.lblBlue = New System.Windows.Forms.Label()
Me.Button1 = New System.Windows.Forms.Button()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.TextBox2 = New System.Windows.Forms.TextBox()
Me.f1 = New System.Windows.Forms.Label()
Me.c1 = New System.Windows.Forms.Label()
Me.Button2 = New System.Windows.Forms.Button()
Me.TrackBar1 = New System.Windows.Forms.TrackBar()
CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'hs
'
Me.hs.Anchor = System.Windows.Forms.AnchorStyles.None
Me.hs.Location = New System.Drawing.Point(72, 192)
Me.hs.Name = "hs"
Me.hs.Size = New System.Drawing.Size(400, 17)
Me.hs.TabIndex = 0
'
'lblRed
'
Me.lblRed.Anchor = System.Windows.Forms.AnchorStyles.None
Me.lblRed.BackColor = System.Drawing.SystemColors.ControlText
Me.lblRed.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblRed.ForeColor = System.Drawing.Color.Lime
Me.lblRed.Location = New System.Drawing.Point(368, 80)
Me.lblRed.Name = "lblRed"
Me.lblRed.TabIndex = 1
Me.lblRed.Text = "Celcius"
Me.lblRed.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'lblBlue
'
Me.lblBlue.Anchor = System.Windows.Forms.AnchorStyles.None
Me.lblBlue.BackColor = System.Drawing.SystemColors.ControlText
Me.lblBlue.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblBlue.ForeColor = System.Drawing.Color.Lime
Me.lblBlue.Location = New System.Drawing.Point(72, 80)
Me.lblBlue.Name = "lblBlue"
Me.lblBlue.TabIndex = 2
Me.lblBlue.Text = "Farenheit"
Me.lblBlue.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Button1
'
Me.Button1.Anchor = System.Windows.Forms.AnchorStyles.None
Me.Button1.BackColor = System.Drawing.SystemColors.ControlText
Me.Button1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Button1.ForeColor = System.Drawing.Color.Red
Me.Button1.Location = New System.Drawing.Point(240, 136)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(88, 23)
Me.Button1.TabIndex = 5
Me.Button1.Text = "&Convert >>"
'
'TextBox1
'
Me.TextBox1.Anchor = System.Windows.Forms.AnchorStyles.None
Me.TextBox1.BackColor = System.Drawing.SystemColors.ControlText
Me.TextBox1.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TextBox1.ForeColor = System.Drawing.Color.Yellow
Me.TextBox1.Location = New System.Drawing.Point(72, 136)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.TabIndex = 6
Me.TextBox1.Text = ""
Me.TextBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
'
'TextBox2
'
Me.TextBox2.Anchor = System.Windows.Forms.AnchorStyles.None
Me.TextBox2.BackColor = System.Drawing.SystemColors.ControlText
Me.TextBox2.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TextBox2.ForeColor = System.Drawing.Color.Yellow
Me.TextBox2.Location = New System.Drawing.Point(368, 136)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.TabIndex = 7
Me.TextBox2.Text = ""
Me.TextBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Right
'
'f1
'
Me.f1.Anchor = System.Windows.Forms.AnchorStyles.None
Me.f1.BackColor = System.Drawing.SystemColors.ControlText
Me.f1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.f1.ForeColor = System.Drawing.Color.Magenta
Me.f1.Location = New System.Drawing.Point(72, 104)
Me.f1.Name = "f1"
Me.f1.TabIndex = 9
Me.f1.Text = "Farenheit"
Me.f1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'c1
'
Me.c1.Anchor = System.Windows.Forms.AnchorStyles.None
Me.c1.BackColor = System.Drawing.SystemColors.ControlText
Me.c1.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.c1.ForeColor = System.Drawing.Color.Magenta
Me.c1.Location = New System.Drawing.Point(368, 104)
Me.c1.Name = "c1"
Me.c1.TabIndex = 8
Me.c1.Text = "Celcius"
Me.c1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Button2
'
Me.Button2.Anchor = System.Windows.Forms.AnchorStyles.None
Me.Button2.BackColor = System.Drawing.SystemColors.ControlText
Me.Button2.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Button2.ForeColor = System.Drawing.Color.Red
Me.Button2.Location = New System.Drawing.Point(240, 80)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(88, 23)
Me.Button2.TabIndex = 10
Me.Button2.Text = "Close &X"
'
'TrackBar1
'
Me.TrackBar1.AccessibleRole = System.Windows.Forms.AccessibleRole.Animation
Me.TrackBar1.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(224, Byte), CType(192, Byte))
Me.TrackBar1.Cursor = System.Windows.Forms.Cursors.Hand
Me.TrackBar1.Dock = System.Windows.Forms.DockStyle.Top
Me.TrackBar1.Name = "TrackBar1"
Me.TrackBar1.Size = New System.Drawing.Size(568, 45)
Me.TrackBar1.TabIndex = 11
Me.TrackBar1.TickStyle = System.Windows.Forms.TickStyle.Both
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.SystemColors.ActiveCaptionText
Me.ClientSize = New System.Drawing.Size(568, 266)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TrackBar1, Me.Button2, Me.f1, Me.c1, Me.TextBox2, Me.TextBox1, Me.Button1, Me.lblBlue, Me.lblRed, Me.hs})
Me.ForeColor = System.Drawing.Color.Yellow
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub hs_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles hs.Scroll
Dim TempF, TempC As Double
'Read F and convert to C - divide by 10 needed as value is 10ths of degrees
TempF = (Val(hs.Value) / 10)
'check to see if changed from hot to cold or vice-versa
f1.Text = Format(TempF, "0.00")
TempC = (TempF - 32) * 5 / 9
c1.Text = Format(TempC, "0.00")
If c1.Text = 100 Then
MsgBox("IT HAS REACHED BOILING POINT NOW !!!")
End If
If c1.Text = 0 Then
MsgBox("IT HAS REACHED FREEZING POINT NOW !!!")
End If
If c1.Text = 37.11 Then
MsgBox("IT IS NORMAL BODY TEMPERATURE OF HUMAN BEINGS !!!")
End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim c, f As Double
f = Val(TextBox1.Text)
c = (f - 32) * 5 / 9
TextBox2.Text = "" & Format(c, "0.00")
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
hs.Maximum = 2129
hs.Minimum = 0
TrackBar1.Maximum = 5000
TrackBar1.Minimum = 0
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub

Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll
Dim TempF, TempC As Double
'Read F and convert to C - divide by 10 needed as value is 10ths of degrees
TempF = (Val(TrackBar1.Value) / 10)
'check to see if changed from hot to cold or vice-versa
f1.Text = Format(TempF, "0.00")
TempC = (TempF - 32) * 5 / 9
c1.Text = Format(TempC, "0.00")
If c1.Text = 100 Then
MsgBox("IT HAS REACHED BOILING POINT NOW !!!")
End If
If c1.Text = 0 Then
MsgBox("IT HAS REACHED FREEZING POINT NOW !!!")
End If
If c1.Text = 37.11 Then
MsgBox("IT IS NORMAL BODY TEMPERATURE OF HUMAN BEINGS !!!")
End If
End Sub
End Class







Did you like this resource? Share it with your friends and show your love!

Responses to "Celsius to Farenheit Conversion and vice versa"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Jayesh
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India