Sabtu, 01 Juni 2013

Program Perhitungan Rumus Phytagoras dengan menggunakan Bahasa Pemograman Microsoft Visual Basic 6.0

Rumus Phytagoras atau biasa disebut dengan Rumus abc digunakan untuk mencari panjang sisi - sisi segitiga siku - siku.  
 
Rumus Phyatgoras  :
c2 = a2 + b2
a2 = c2 - b2
b2 = c2 - a2

Langkah – langkah pembuatan program :
·           Buka proyek Standard EXE yang baru
·           Disain tampilan form seperti gambar berikut dengan menggunakan objek Label dan TextBox dan CommandButton

 Gbr 1

·      Atur Properties masing - masing kontrol seperti dibawah ini :

Kontrol
Properties
Pengaturan
Option1
Caption
Hitung nilai a
Option2
Caption
Hitung nilai b
Option3
Caption
Hitung nilai c
TextBox1
Name       
Text     
txt_A
[dikosongkan]
TextBox2
Name       
Text     
txt_B
[dikosongkan]
TextBox2
Name       
Text     
txt_C
[dikosongkan]
Label1
Name
Caption
Appearance
Border Style
lbl_jawaban
[dikosongkan]
0 - Flat
1 - Fixed Single
Label2
Caption
Sisi a (AB)
Label3
Caption
Sisi b (BC)
Label4
Caption
Sisi c (AC)
CommandButton1
Name
Caption
cmd_hitung
&Hitung
CommandButton2
Name
Caption
cmd_exit
E&xit

Form1
Name
Caption
Frm_Phytagoras
Rumus Phytagoras

Catatan    :    Untuk jenis, ukuran dan warna Font serta warna Form sesuaikan dengan selera anda.
Nilai Left dan Top objek label_a, label_b, label_c, txt_A,txt_B dan txt_C sesuaikan dengan nilai masing - masing objek pada Properties

·           Klik menu View  =>  Code, lalu kode program di bawah ini :

Dim A, B, C As Integer
Private Sub cmd_exit_Click()
End
End Sub

Private Sub cmd_hitung_Click()
If Me.Option1.Value = True Then
    B = Me.txt_B.Text
    C = Me.txt_C.Text
    Me.lbl_jawaban.Caption = Format(Sqr((C ^ 2) - (B ^ 2)), "###,###.###")
ElseIf Me.Option2.Value = True Then
    A = Me.txt_A.Text
    C = Me.txt_C.Text
    Me.lbl_jawaban.Caption = Format(Sqr((C ^ 2) - (A ^ 2)), "###,###.###")
ElseIf Me.Option3.Value = True Then
    A = Me.txt_A.Text
    B = Me.txt_B.Text
    Me.lbl_jawaban.Caption = Format(Sqr((A ^ 2) + (B ^ 2)), "###,###.###")
End If
Me.cmd_hitung.Enabled = False
End Sub

Private Sub Form_Load()
Me.cmd_hitung.Enabled = False
End Sub
Private Sub Option1_Click()
Me.label_a.Left = 120
Me.label_a.Top = 1200
Me.label_b.Left = 120
Me.label_b.Top = 240
Me.label_c.Left = 120
Me.label_c.Top = 720

Me.txt_A.Left = 1200
Me.txt_A.Top = 1200
Me.txt_B.Left = 1200
Me.txt_B.Top = 240
Me.txt_C.Left = 1200
Me.txt_C.Top = 720

Me.txt_A.Text = ""
Me.txt_B.Text = ""
Me.txt_C.Text = ""

Me.txt_A.Visible = False
Me.txt_B.Visible = True
Me.txt_C.Visible = True
Me.lbl_jawaban.Visible = True
Me.lbl_jawaban.Caption = ""

Me.txt_A.Enabled = False
Me.txt_B.Enabled = True
Me.txt_B.SetFocus
Me.txt_C.Enabled = False
End Sub

Private Sub Option2_Click()
'nilai Left dan Top objek label_a, label_b, label_c, txt_A,txt_B dan txt_C
'sesuaikan dengan nilai masing - masing objek pada Properties
Me.label_a.Left = 120
Me.label_a.Top = 240
Me.label_b.Left = 120
Me.label_b.Top = 1200
Me.label_c.Left = 120
Me.label_c.Top = 720

Me.txt_A.Left = 1200
Me.txt_A.Top = 240
Me.txt_B.Left = 1200
Me.txt_B.Top = 1200
Me.txt_C.Left = 1200
Me.txt_C.Top = 720

Me.txt_A.Text = ""
Me.txt_B.Text = ""
Me.txt_C.Text = ""

Me.txt_A.Visible = True
Me.txt_B.Visible = False
Me.txt_C.Visible = True
Me.lbl_jawaban.Visible = True
Me.lbl_jawaban.Caption = ""

Me.txt_A.Enabled = True
Me.txt_A.SetFocus
Me.txt_B.Enabled = False
Me.txt_C.Enabled = False
End Sub
Private Sub Option3_Click()
Me.label_a.Left = 120
Me.label_a.Top = 240
Me.label_b.Left = 120
Me.label_b.Top = 720
Me.label_c.Left = 120
Me.label_c.Top = 1200

Me.txt_A.Left = 1200
Me.txt_A.Top = 240
Me.txt_B.Left = 1200
Me.txt_B.Top = 720
Me.txt_C.Left = 1200
Me.txt_C.Top = 1200

Me.txt_A.Text = ""
Me.txt_B.Text = ""
Me.txt_C.Text = ""

Me.txt_A.Visible = True
Me.txt_B.Visible = True
Me.txt_C.Visible = False
Me.lbl_jawaban.Visible = True
Me.lbl_jawaban.Caption = ""

Me.txt_A.Enabled = True
Me.txt_A.SetFocus
Me.txt_B.Enabled = False
Me.txt_C.Enabled = False
End Sub

Private Sub txt_A_KeyPress(KeyAscii As Integer)
Me.txt_A.MaxLength = 2
If KeyAscii = 13 Then
    If Me.txt_A.Text = "" Then
        MsgBox "Data tidak boleh kosong", vbInformation + vbOKOnly, "Data Kosong"
    Else
        If Me.Option2.Value = True Then
            Me.txt_C.Enabled = True
            Me.txt_C.SetFocus
        ElseIf Me.Option3.Value = True Then
            Me.txt_B.Enabled = True
            Me.txt_B.SetFocus
        End If
    End If
End If
End Sub
Private Sub txt_B_KeyPress(KeyAscii As Integer)
Me.txt_B.MaxLength = 2
If KeyAscii = 13 Then
    If Me.txt_B.Text = "" Then
        MsgBox "Data tidak boleh kosong", vbInformation + vbOKOnly, "Data Kosong"
    Else
        If Me.Option3.Value = True Then
            If Val(Me.txt_B.Text) <= Val(Me.txt_A.Text) Then
                MsgBox "Data Salah, nilai B harus lebih besar dari nilai A", vbInformation + vbOKOnly, "Data Kosong"
                Me.txt_B.Text = ""
            Else
                Me.cmd_hitung.Enabled = True
                Me.cmd_hitung.SetFocus
            End If
        ElseIf Me.Option1.Value = True Then
            Me.txt_C.Enabled = True
            Me.txt_C.SetFocus
        End If
    End If
End If
End Sub

Private Sub txt_C_KeyPress(KeyAscii As Integer)
Me.txt_C.MaxLength = 2
If KeyAscii = 13 Then
    If Me.txt_C.Text = "" Then
        MsgBox "Data tidak boleh kosong", vbInformation + vbOKOnly, "Data Kosong"
    Else
        If Me.Option1.Value = True Then
            If Val(Me.txt_C.Text) <= Val(Me.txt_B.Text) Then
                MsgBox "Data Salah, nilai C harus lebih besar dari nilai B", vbInformation + vbOKOnly, "Data Kosong"
                Me.txt_C.Text = ""
            Else
                Me.cmd_hitung.Enabled = True
                Me.cmd_hitung.SetFocus
            End If
        ElseIf Me.Option2.Value = True Then
            If Val(Me.txt_C.Text) <= Val(Me.txt_A.Text) Then
                MsgBox "Data Salah, nilai C harus lebih besar dari nilai A", vbInformation + vbOKOnly, "Data Kosong"
                Me.txt_C.Text = ""
            Else
                Me.cmd_hitung.Enabled = True
                Me.cmd_hitung.SetFocus
            End If
        End If
    End If
End If
End Sub

·           Jalankan Program dengan cara menekan tombol F5 pada keyboard atau pilih menu Run  => Start
·           Hasilnya ketika program dijalankan :
 

 Gbr 2




Download Source Codenya  disini atau disini

Tidak ada komentar:

Posting Komentar

Pos kan komentar anda !