Senin, 12 November 2012

Tugas 3 Pemrograman Visual

Modifikasi Program

Soal 1
1.       Saat form dijalankan : semua form tidak aktif, tombol isi data dan tutup aktif, tombol clear tidak aktif
2.       Saat ditekan tombol isi data : kodebarang, jumlahbarang, cara beli, tombol clear aktif namun tmbol isi data tidak aktif
3.       Saat ditekan tombol clear sama dengan saat form dijalankan
b
 buat desain seperti berikut



dan kemudian 
Dobel klik pada form dan kemudian ketikkan kode seperti berikut
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ComboBox1.Items.Add("SPT")
        ComboBox1.Items.Add("SND")
        ComboBox1.Items.Add("TST")
        ComboBox1.Items.Add("TOP")
        ComboBox1.Items.Add("TAS")

        Dim control As Windows.Forms.Control
        For Each control In Me.Controls
            control.Enabled = False
            Button3.Enabled = True
            Button1.Enabled = True
        Next
    End Sub

Dobel klikpada combobox dan kemudian ketikkan code seperti dibawah ini;
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim kodebarang, namabarang As String
        Dim harga As String
        kodebarang = ComboBox1.Text

        Select Case kodebarang
            Case "SPT"
                namabarang = "SEPATU"
                harga = 200000
            Case "SND"
                namabarang = "SENDAL"
                harga = 100000
            Case "TST"
                namabarang = "T-SHIRT"
                harga = 150000
            Case "TOP"
                namabarang = "TOPI"
                harga = 50000
            Case "TAS"
                namabarang = "TAS"
                harga = 250000
        End Select
        Textnamabarang.Text = namabarang
        Texthargabarang.Text = harga
    End Sub

Dobel klik pada textbox jumlah barang dan ketikkan code seperti berikut
    Private Sub Textjumlahbarang_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Textjumlahbarang.TextChanged
        Texttotal.Text = Val(Texthargabarang.Text) * Val(Textjumlahbarang.Text)
        Textbayar.Text = Val(Texttotal.Text) - Val(Textdiskon.Text)
    End Sub

Dobel klik pada button2 (clear) dan kemudian ketikkan kode seperti berikut
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim b As Windows.Forms.Control
        For Each b In Me.Controls
            b.Enabled = False
            Button3.Enabled = True
        Next
        Textbayar.Clear()
        Textdiskon.Clear()
        Texthargabarang.Clear()
        Textjumlahbarang.Clear()
        Textnamabarang.Clear()
        Texttotal.Clear()
        Me.RadioButton1.Checked = False
        Me.RadioButton2.Checked = False
        ComboBox1.Text = ""
    End Sub

Dobel klik pada button3 (tutup) dan kemudian ketikkan kode seperti berikut
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If MsgBox("Anda Yakin Ingin keluar?", vbYesNo + vbQuestion + vbDefaultButton3, "keluar") = vbYes Then
            Me.Close()
        End If
    End Sub

Dobel klik pada button1 (isi data) dan kemudian ketikkan kode seperti berikut
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim kendali As Windows.Forms.Control
        For Each kendali In Me.Controls
            kendali.Enabled = True
            Button1.Enabled = False
        Next
    End Sub

Dobel klik pada radio button1 (tunai) dan kemudian ketikkan kode seperti berikut
    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        Textdiskon.Text = Val(Texttotal.Text) * 10 / 100
        Textbayar.Text = Val(Textbayar.Text) - Val(Textdiskon.Text)
    End Sub

 Dobel klik pada radio button2 (kredit) dan kemudian ketikkan kode seperti berikut
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
        Textdiskon.Text = 0
        Textbayar.Text = Val(Texttotal.Text) - Val(Textdiskon.Text)
    End Sub
End Class
word

pada saat program dijalankan maka akan tampil seperti berikut


           Pada gambar diatas tombol yang aktif hanyalah tombol isi data dan tombol tutup, yang lainnya mati (tidak berfungsi), hal tersebut dikarenakan code program berikut
Dim control As Windows.Forms.Control
        For Each control In Me.Controls
            control.Enabled = False
            Button3.Enabled = True
            Button1.Enabled = True
Dan setelah tombol  isi data diklik maka tampilan  akan berubah menjadi seperti berikut







 Soal 2
Membuat game puzzle dengan scenario permaian sebagai berikut
1.       Saat form dijalankan posisi angka acak
2.       Saat ditekan tombol yang terletk didekat tombol kosong maka tombol tersebut kosong, begitu seterusnya dengan tersusun dari 1 sampai 8
3.       Saat angka sudah tersusun tampilkan pesan “Selamat Anda Berhasil”

bbuat desain seperti pada gambar berikut



kemudian
Pada button 1 ketikkan kode serti berikut
    Dim puzzle As String
  
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        puzzle = Button1.Text
        If (Button2.Text = "") Then
            Button1.Text = ""
            Button2.Text = puzzle
        ElseIf (Button4.Text = "") Then
            Button1.Text = ""
            Button4.Text = puzzle
        End If
        periksa()
    End Sub


Pada button 2 ketikkan kode serti berikut

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        puzzle = Button2.Text
        If (Button1.Text = "") Then
            Button2.Text = ""
            Button1.Text = puzzle
        ElseIf (Button3.Text = "") Then
            Button2.Text = ""
            Button3.Text = puzzle
        ElseIf (Button5.Text = "") Then
            Button2.Text = ""
            Button5.Text = puzzle
        End If
        periksa()
    End Sub


Pada button 3 ketikkan kode serti berikut

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        puzzle = Button3.Text
        If (Button2.Text = "") Then
            Button3.Text = ""
            Button2.Text = puzzle
        ElseIf (Button6.Text = "") Then
            Button3.Text = ""
            Button6.Text = puzzle
        End If
        periksa()
    End Sub


Pada button 4 ketikkan kode serti berikut

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        puzzle = Button4.Text
        If (Button1.Text = "") Then
            Button4.Text = ""
            Button1.Text = puzzle
        ElseIf (Button5.Text = "") Then
            Button4.Text = ""
            Button5.Text = puzzle
        ElseIf (Button7.Text = "") Then
            Button4.Text = ""
            Button7.Text = puzzle
        End If
        periksa()
    End Sub


Pada button 5 ketikkan kode serti berikut

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        puzzle = Button5.Text
        If (Button2.Text = "") Then
            Button5.Text = ""
            Button2.Text = puzzle
        ElseIf (Button6.Text = "") Then
            Button5.Text = ""
            Button6.Text = puzzle
        ElseIf (Button8.Text = "") Then
            Button5.Text = ""
            Button8.Text = puzzle
        ElseIf (Button4.Text = "") Then
            Button5.Text = ""
            Button4.Text = puzzle
        End If
        periksa()
    End Sub


Pada button 6 ketikkan kode serti berikut

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        puzzle = Button6.Text
        If (Button3.Text = "") Then
            Button6.Text = ""
            Button3.Text = puzzle
        ElseIf (Button5.Text = "") Then
            Button6.Text = ""
            Button5.Text = puzzle
        ElseIf (Button9.Text = "") Then
            Button6.Text = ""
            Button9.Text = puzzle
        End If
        periksa()
    End Sub


Pada button 7 ketikkan kode serti berikut

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        puzzle = Button7.Text
        If (Button4.Text = "") Then
            Button7.Text = ""
            Button4.Text = puzzle
        ElseIf (Button8.Text = "") Then
            Button7.Text = ""
            Button8.Text = puzzle
        ElseIf (Button9.Text = "") Then
            Button7.Text = ""
            Button9.Text = ""
        End If
        periksa()
    End Sub


Pada button 8 ketikkan kode serti berikut

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        puzzle = Button8.Text
        If (Button5.Text = "") Then
            Button8.Text = ""
            Button8.Text = puzzle
        ElseIf (Button7.Text = "") Then
            Button8.Text = ""
            Button7.Text = puzzle
        ElseIf (Button9.Text = "") Then
            Button8.Text = ""
            Button9.Text = puzzle
        End If
    End Sub


Pada button 9 ketikkan kode serti berikut

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        puzzle = Button9.Text
        If (Button6.Text = "") Then
            Button9.Text = ""
            Button6.Text = ""
        ElseIf (Button8.Text = "") Then
            Button9.Text = ""
            Button8.Text = puzzle
        End If
        periksa()
    End Sub

    Private Sub periksa()
        If (Button1.Text = "1" And Button1.Text = "2" And Button3.Text = "3" And Button4.Text = "4") Then
            If (Button5.Text = "5" And Button6.Text = "6" And Button7.Text = "7") Then
                If (Button8.Text = "8") Then
                    MsgBox("ANDA BERHASIL!!!")
                End If
            End If
        End If
    End Sub


Kemudian pada button 10 (Berhenti)  ketikkan kode serti berikut

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Timer1.Stop()
 Button1.Text = "4"
        Button2.Text = "7"
        Button3.Text = "8"
        Button4.Text = "1"
        Button5.Text = "2"
        Button6.Text = "3"
        Button7.Text = "6"
        Button8.Text = "5"
        Button9.Text = ""


    End Sub


Dobel klik pada timer dan kemudian ketikkan kode seperti berikut
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        a = (Rnd() * 7) + 1
        b = (Rnd() * 7) + 1
        c = (Rnd() * 7) + 1
        d = (Rnd() * 7) + 1
        f = (Rnd() * 7) + 1
        g = (Rnd() * 7) + 1
        h = (Rnd() * 7) + 1
        i = (Rnd() * 7) + 1

        Button1.Text = a
        Button2.Text = b
        Button3.Text = c
        Button4.Text = d
        Button5.Text = f
        Button6.Text = g
        Button7.Text = h
        Button8.Text = i


    End Sub


Pada button 11 (Keluar)  ketikkan kode serti berikut

    Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
        If MsgBox("Yakin Anda Ingin Keluar?", vbYesNo + vbQuestion + vbDefaultButton1, "Keluar dari Puzzle") = vbYes Then
            Me.Close()
        End If
    End Sub
    Dim a, b, c, d, e, f, g, h, i As Integer

  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Enabled = True
    End Sub
End Class

Pada saat program dijalankan maka angka akan terus berjalan acak, akan berhenti apabila kita telah menekan tombol berhenti. Berikut tampilannya


Dan apabila kita sudah berhasil menyusun secara urut akan akan tampil messagebox seperti pada gambar dibawah iini


Demikian, terima kasih



Tidak ada komentar:

Posting Komentar

Follow @dhoelzroman