Kamis, 26 Desember 2013

“Temperature Conversion” dengan Visual Basic.NET 2010

Temperature Conversion VB.NET

Seperti yang sudah saya janjikan sebelumnya, inilah hasil akhir adaptasi "Temperature Conversion” yang sebelumnya saya terapkan pada Visual Basic 6.0. Tetap dengan rumusan yang sama, oleh karena itulah seperti yang dapat teman-teman lihat pada gambar di samping bahwa konversi suhu menggunakan Fahrenheit kurang sempurna. Oleh karena itu sudi bilanys teman-teman membantu saya mengembangkan aplikasi ini, terutama pada rumus konversi fahrenheit. Saya sangat senang sekali. Dan bila teman-teman lihat pada codingnya tidak jauh berbeda dengan pembuatan yang sebelumnya. Hanya masalah adaptasi format setting pada kontrol saja dan saya yakin teman-teman tidak kebingungan, Trims.

1. Jalankan terlebih dahulu aplikasi Visual Basic 2010

2. Pada tabulasi “Start Page” pilih “New Project” atau bisa juga dengan menekan tombol “Ctrl + N” pada keyboard

3. Pada jendela “New Project” pilih “Windows Forms Application” kemudian tekan tombol “OK

4. Berikut setingan kontrol properti yang terdapat pada aplikasi ini

Object Name Properties Setting
Form Form1 Font Tahoma; 10pt
    Form Border Style Fixed Single
    Maximize Box False
    Minimize Box False
    Start Position Center Screen
    Text Temperature Conversion
Radio Button optCelsius Text Celsius
Radio Button optFahrenheit Text Fahrenheit
Radio Button optKelvin Text Kelvin
Group Box GroupBox1 Text (Empty)
Label Label1 Text Degree:
Text Box txtUserInput Font Tahoma; 14pt
    Max Length 7
    Text Align Right
Label Label2 Auto Size False
    Text Press "Enter" to start Conversion
Label lblDegreeConversion Auto Size False
    Font Tahoma; 36pt
    Text (Empty)
    Text Align Middle Center
Label lblInfo Auto Size False
    Text (Empty)
    Text Align Middle Center
Label lblDisplayConversion1 Auto Size False
    Text (Empty)
    Text Align Middle Center
Label lblConversion1 Auto Size False
    Back Color White
    Border Style Fixed3D
    Font Tahoma; 14pt
    Text (Empty)
    Text Align Middle Center
Label lblDisplayConversion2 Auto Size False
    Text (Empty)
    Text Align Middle Center
Label lblConversion2 Auto Size False
    Back Color White
    Border Style Fixed3D
    Font Tahoma; 14pt
    Text (Empty)
    Text Align Middle Center
Line Shape LineShape1 Border Width 1
Line Shape LineShape2 Border Width 2
    Border Color White
Button cmdExit Text E&xit

5. Berikut event code program

Public Class Form1
    Dim Coice As String

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If MsgBox("Exit the Aplication?", vbYesNo + vbQuestion, "Confirmation") = vbNo Then
            If e.CloseReason = CloseReason.UserClosing Then
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Close()
    End Sub

    Private Sub StarConverter()
        txtUserInput.Text = ""
        lblDegreeConversion.Text = ""
        lblConversion2.Text = ""
        lblConversion1.Text = ""
        lblInfo.Text = ""
        txtUserInput.Focus()
    End Sub

    Private Sub optCelsius_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles optCelsius.Click
        StarConverter()
        lblDisplayConversion1.Text = "Fahrenheit:"
        lblDisplayConversion2.Text = "Kelvin:"
        Coice = "Celsius"
    End Sub

    Private Sub optFahrenheit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles optFahrenheit.Click
        StarConverter()
        lblDisplayConversion1.Text = "Kelvin:"
        lblDisplayConversion2.Text = "Celsius:"
        Coice = "Fahrenheit"
    End Sub

    Private Sub optKelvin_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles optKelvin.Click
        StarConverter()
        lblDisplayConversion1.Text = "Celsius:"
        lblDisplayConversion2.Text = "Fahrenheit:"
        Coice = "Kelvin"
    End Sub

    Private Sub txtUserInput_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtUserInput.KeyPress
        If Not (e.KeyChar >= ChrW(Keys.D0) And e.KeyChar <= ChrW(Keys.D9) Or e.KeyChar = ChrW(45) Or e.KeyChar = ChrW(46) Or e.KeyChar = ChrW(Keys.Back) Or e.KeyChar = ChrW(Keys.Return)) Then
            e.KeyChar = ChrW(0)
        ElseIf e.KeyChar = ChrW(Keys.Return) Then
            Select Case Coice
                Case "Celsius"
                    lblDegreeConversion.Text = Val(txtUserInput.Text)
                    lblConversion1.Text = CDbl(Val(txtUserInput.Text) * 1.8 + 32)
                    lblConversion2.Text = CDbl(Val(txtUserInput.Text) + 273.15)
                    txtUserInput.Text = ""
                    If lblDegreeConversion.Text = -273.15 Then
                        lblInfo.Text = "Absolute Zero"
                    ElseIf lblDegreeConversion.Text = 0 Then
                        lblInfo.Text = "Water Freezing"
                    ElseIf lblDegreeConversion.Text = 37 Then
                        lblInfo.Text = "Human Body Temperature"
                    ElseIf lblDegreeConversion.Text = 100 Then
                        lblInfo.Text = "Water boils"
                    Else
                        lblInfo.Text = ""
                    End If
                Case "Fahrenheit"
                    lblDegreeConversion.Text = Val(txtUserInput.Text)
                    lblConversion1.Text = CDbl((Val(txtUserInput.Text) + 459.67) * 5 \ 9)
                    lblConversion2.Text = CDbl(Val(txtUserInput.Text - 32) * 5 \ 9)
                    txtUserInput.Text = ""
                    If lblDegreeConversion.Text = -459.67 Then
                        lblInfo.Text = "Absolute Zero"
                    ElseIf lblDegreeConversion.Text = 32 Then
                        lblInfo.Text = "Water Freezing"
                    ElseIf lblDegreeConversion.Text = 98.6 Then
                        lblInfo.Text = "Human Body Temperature"
                    ElseIf lblDegreeConversion.Text = 212 Then
                        lblInfo.Text = "Water boils"
                    Else
                        lblInfo.Text = ""
                    End If
                Case "Kelvin"
                    lblDegreeConversion.Text = Val(txtUserInput.Text)
                    lblConversion1.Text = CDbl(Val(txtUserInput.Text) - 273.15)
                    lblConversion2.Text = CDbl(Val(txtUserInput.Text) * 1.8 - 459.67)
                    txtUserInput.Text = ""
                    If lblDegreeConversion.Text = 0 Then
                        lblInfo.Text = "Absolute Zero"
                    ElseIf lblDegreeConversion.Text = 273.15 Then
                        lblInfo.Text = "Water Freezing"
                    ElseIf lblDegreeConversion.Text = 310.15 Then
                        lblInfo.Text = "Human Body Temperature"
                    ElseIf lblDegreeConversion.Text = 373.15 Then
                        lblInfo.Text = "Water boils"
                    Else
                        lblInfo.Text = ""
                    End If
            End Select
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.optCelsius.Checked = True
    End Sub
End Class

Label:

0 Komentar:

Posting Komentar

Berlangganan Posting Komentar [Atom]

<< Beranda