Sabtu, 28 Desember 2013

“Unit Conversion” dengan Visual Basic.NET 2010

Unit Conversion VB.NET

Seperti biasa setelah sebelumnya aplikasi ini saya terapkan pada Visual Basic 6.0 kini saya adaptasikan pada Visual Basic.NET 2010. Dan seperti posting saya yang sebelumnya tidak ada perbedaan baik nama kontrol dan logika programnya. Mungkin yang membedakan pada segi implementasi perintah saja. Karena seperti yang saya sudah saya informasikan pada postingan sebelumnya, maksud dan tujuan saya membuat blog ini yaitu untuk membedakan kedua aplikasi pemrogaman ini dan dimana letak perbedaannya.

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 Unit Conversion
Radio Button optLength Check Align Middle Right
    Text Length Conversion
Radio Button optWeight Check Align Middle Right
    Text Weight Conversion
Radio Button optVolume Check Align Middle Right
    Text Volume Conversion
Group Box GroupBox1 Text (Empty)
Label lblDisplayUnit Text (Empty)
Text Box txtUserInput Font Tahoma; 14pt
    Max Length 7
    Text Align Center
Label Label1 Text =
Label Label2 Text Result:
Label lblResult Auto Size False
    Back Color White
    Border Style Fixed3D
    Font Tahoma; 14pt
    Text (Empty)
    Text Align MiddleCenter
List Box lstFrom    
List Box lstTo    
Button cmdConvert Text Convert
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 tmpList As Integer

    Private Sub Clear()
        txtUserInput.Text = ""
        lblResult.Text = ""
        txtUserInput.Focus()
        lstFrom.Items.Clear()
        lstTo.Items.Clear()
    End Sub

    Private Sub optLength_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles optLength.Click
        lblDisplayUnit.Text = "Unit length:"
        Clear()

        lstFrom.Items.Add("Kilometer (km)")
        lstFrom.Items.Add("Hektometer (hm)")
        lstFrom.Items.Add("Dekameter (dam)")
        lstFrom.Items.Add("Meter (m)")
        lstFrom.Items.Add("Desimeter (dm)")
        lstFrom.Items.Add("Centimeter (cm)")
        lstFrom.Items.Add("Milimeter (mm)")

        For Me.tmpList = 0 To (lstFrom.Items.Count - 1)
            lstTo.Items.Add(lstFrom.Items.Item(tmpList))
        Next
    End Sub

    Private Sub optWeight_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optWeight.CheckedChanged
        lblDisplayUnit.Text = "Unit Weight:"
        Clear()

        lstFrom.Items.Add("Kilogram (kg)")
        lstFrom.Items.Add("Hektogram (hg)")
        lstFrom.Items.Add("Dekagram (dag)")
        lstFrom.Items.Add("Gram (g)")
        lstFrom.Items.Add("Desigram (dg)")
        lstFrom.Items.Add("Centigram (cg)")
        lstFrom.Items.Add("Miligram (mg)")

        For Me.tmpList = 0 To (lstFrom.Items.Count - 1)
            lstTo.Items.Add(lstFrom.Items.Item(tmpList))
        Next
    End Sub

    Private Sub optVolume_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optVolume.CheckedChanged
        lblDisplayUnit.Text = "Unit Volume:"
        Clear()

        lstFrom.Items.Add("Kiloliter (kl)")
        lstFrom.Items.Add("Hektoliter (hl)")
        lstFrom.Items.Add("Dekaliter (dal)")
        lstFrom.Items.Add("Liter (l)")
        lstFrom.Items.Add("Desiliter (dl)")
        lstFrom.Items.Add("Centiliter (cl)")
        lstFrom.Items.Add("Mililiter (ml)")

        For Me.tmpList = 0 To (lstFrom.Items.Count - 1)
            lstTo.Items.Add(lstFrom.Items.Item(tmpList))
        Next
    End Sub

    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 cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click
        Close()
    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)
        End If
    End Sub

    Private Sub cmdConvert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConvert.Click
        If lstFrom.GetSelected(0) And lstTo.GetSelected(0) Then
            lblResult.Text = Val(txtUserInput.Text)
        ElseIf lstFrom.GetSelected(0) And lstTo.GetSelected(1) Then
            lblResult.Text = Val(txtUserInput.Text) * 10
        ElseIf lstFrom.GetSelected(0) And lstTo.GetSelected(2) Then
            lblResult.Text = Val(txtUserInput.Text) * 100
        ElseIf lstFrom.GetSelected(0) And lstTo.GetSelected(3) Then
            lblResult.Text = Val(txtUserInput.Text) * 1000
        ElseIf lstFrom.GetSelected(0) And lstTo.GetSelected(4) Then
            lblResult.Text = Val(txtUserInput.Text) * 10000
        ElseIf lstFrom.GetSelected(0) And lstTo.GetSelected(5) Then
            lblResult.Text = Val(txtUserInput.Text) * 100000
        ElseIf lstFrom.GetSelected(0) And lstTo.GetSelected(6) Then
            lblResult.Text = Val(txtUserInput.Text) * 1000000
        End If

        If lstFrom.GetSelected(1) And lstTo.GetSelected(0) Then
            lblResult.Text = Val(txtUserInput.Text) / 10
        ElseIf lstFrom.GetSelected(1) And lstTo.GetSelected(1) Then
            lblResult.Text = Val(txtUserInput.Text)
        ElseIf lstFrom.GetSelected(1) And lstTo.GetSelected(2) Then
            lblResult.Text = Val(txtUserInput.Text) * 10
        ElseIf lstFrom.GetSelected(1) And lstTo.GetSelected(3) Then
            lblResult.Text = Val(txtUserInput.Text) * 100
        ElseIf lstFrom.GetSelected(1) And lstTo.GetSelected(4) Then
            lblResult.Text = Val(txtUserInput.Text) * 1000
        ElseIf lstFrom.GetSelected(1) And lstTo.GetSelected(5) Then
            lblResult.Text = Val(txtUserInput.Text) * 10000
        ElseIf lstFrom.GetSelected(1) And lstTo.GetSelected(6) Then
            lblResult.Text = Val(txtUserInput.Text) * 100000
        End If

        If lstFrom.GetSelected(2) And lstTo.GetSelected(0) Then
            lblResult.Text = Val(txtUserInput.Text) / 100
        ElseIf lstFrom.GetSelected(2) And lstTo.GetSelected(1) Then
            lblResult.Text = Val(txtUserInput.Text) / 10
        ElseIf lstFrom.GetSelected(2) And lstTo.GetSelected(2) Then
            lblResult.Text = Val(txtUserInput.Text)
        ElseIf lstFrom.GetSelected(2) And lstTo.GetSelected(3) Then
            lblResult.Text = Val(txtUserInput.Text) * 10
        ElseIf lstFrom.GetSelected(2) And lstTo.GetSelected(4) Then
            lblResult.Text = Val(txtUserInput.Text) * 100
        ElseIf lstFrom.GetSelected(2) And lstTo.GetSelected(5) Then
            lblResult.Text = Val(txtUserInput.Text) * 1000
        ElseIf lstFrom.GetSelected(2) And lstTo.GetSelected(6) Then
            lblResult.Text = Val(txtUserInput.Text) * 10000
        End If

        If lstFrom.GetSelected(3) And lstTo.GetSelected(0) Then
            lblResult.Text = Val(txtUserInput.Text) / 1000
        ElseIf lstFrom.GetSelected(3) And lstTo.GetSelected(1) Then
            lblResult.Text = Val(txtUserInput.Text) / 100
        ElseIf lstFrom.GetSelected(3) And lstTo.GetSelected(2) Then
            lblResult.Text = Val(txtUserInput.Text) / 10
        ElseIf lstFrom.GetSelected(3) And lstTo.GetSelected(3) Then
            lblResult.Text = Val(txtUserInput.Text)
        ElseIf lstFrom.GetSelected(3) And lstTo.GetSelected(4) Then
            lblResult.Text = Val(txtUserInput.Text) * 10
        ElseIf lstFrom.GetSelected(3) And lstTo.GetSelected(5) Then
            lblResult.Text = Val(txtUserInput.Text) * 100
        ElseIf lstFrom.GetSelected(3) And lstTo.GetSelected(6) Then
            lblResult.Text = Val(txtUserInput.Text) * 1000
        End If

        If lstFrom.GetSelected(4) And lstTo.GetSelected(0) Then
            lblResult.Text = Val(txtUserInput.Text) / 10000
        ElseIf lstFrom.GetSelected(4) And lstTo.GetSelected(1) Then
            lblResult.Text = Val(txtUserInput.Text) / 1000
        ElseIf lstFrom.GetSelected(4) And lstTo.GetSelected(2) Then
            lblResult.Text = Val(txtUserInput.Text) / 100
        ElseIf lstFrom.GetSelected(4) And lstTo.GetSelected(3) Then
            lblResult.Text = Val(txtUserInput.Text) / 10
        ElseIf lstFrom.GetSelected(4) And lstTo.GetSelected(4) Then
            lblResult.Text = Val(txtUserInput.Text)
        ElseIf lstFrom.GetSelected(4) And lstTo.GetSelected(5) Then
            lblResult.Text = Val(txtUserInput.Text) * 10
        ElseIf lstFrom.GetSelected(4) And lstTo.GetSelected(6) Then
            lblResult.Text = Val(txtUserInput.Text) * 100
        End If

        If lstFrom.GetSelected(5) And lstTo.GetSelected(0) Then
            lblResult.Text = Val(txtUserInput.Text) / 100000
        ElseIf lstFrom.GetSelected(5) And lstTo.GetSelected(1) Then
            lblResult.Text = Val(txtUserInput.Text) / 10000
        ElseIf lstFrom.GetSelected(5) And lstTo.GetSelected(2) Then
            lblResult.Text = Val(txtUserInput.Text) / 1000
        ElseIf lstFrom.GetSelected(5) And lstTo.GetSelected(3) Then
            lblResult.Text = Val(txtUserInput.Text) / 100
        ElseIf lstFrom.GetSelected(5) And lstTo.GetSelected(4) Then
            lblResult.Text = Val(txtUserInput.Text) / 10
        ElseIf lstFrom.GetSelected(5) And lstTo.GetSelected(5) Then
            lblResult.Text = Val(txtUserInput.Text)
        ElseIf lstFrom.GetSelected(5) And lstTo.GetSelected(6) Then
            lblResult.Text = Val(txtUserInput.Text) * 10
        End If

        If lstFrom.GetSelected(6) And lstTo.GetSelected(0) Then
            lblResult.Text = Val(txtUserInput.Text) / 1000000
        ElseIf lstFrom.GetSelected(6) And lstTo.GetSelected(1) Then
            lblResult.Text = Val(txtUserInput.Text) / 100000
        ElseIf lstFrom.GetSelected(6) And lstTo.GetSelected(2) Then
            lblResult.Text = Val(txtUserInput.Text) / 10000
        ElseIf lstFrom.GetSelected(6) And lstTo.GetSelected(3) Then
            lblResult.Text = Val(txtUserInput.Text) / 1000
        ElseIf lstFrom.GetSelected(6) And lstTo.GetSelected(4) Then
            lblResult.Text = Val(txtUserInput.Text) / 100
        ElseIf lstFrom.GetSelected(6) And lstTo.GetSelected(5) Then
            lblResult.Text = Val(txtUserInput.Text) / 10
        ElseIf lstFrom.GetSelected(6) And lstTo.GetSelected(6) Then
            lblResult.Text = Val(txtUserInput.Text)
        End If
    End Sub
End Class

Label:

0 Komentar:

Posting Komentar

Berlangganan Posting Komentar [Atom]

<< Beranda