TUGAS 3
Penambahan program if
dan case
Saya akan menjelaskan tentang penambahan tentang program
atau tugas yang sebelumnya:
Penambahannya adalah sebagai berikut:
- Saat program dijalankan agar semua control tidak aktif terkecuali control isi data,dan tutup.
- Kemudian saat tombol isi data diklik atau ditekan tombol kode barang, jumlah barang, cara beli, tombol clear menjadi aktif,sedangkan tombol isi data menjadi tidak aktif
- Dan saat tombol clear ditekan, keadaan program akan menjadi program saat pertama dijalankan
Langkah penambahan:
- Pertama masukan control groupbox kedalam form,kemudian semua control dimasukan kedalam groupbox terkecuali tombol clear dan tutup.cara ini dilakukan untuk mengkelompokan control yang dimasukan digroupbox agar memudahkan penulisan kode program yaitu dengan perulangan,berikut kodenya:
- Langkah selanjutnya menambahkan control atau tombol “isi data” dan saat program dijalankan agar text box kode barang, jumlah barang, cara beli radio buttun tunai dan kredit, dan tombol clear menjadi aktif saat tombol isi data ditekan atau diklik, dan tombol isi data menjadi tidak aktif.kode programnya adalah sebagai berikut:
- Dan langkah yang selanjutnya untuk tombol clear saat diklik sama seperti program saat pertama dijalankan,untuk kodenya adalah sebagai berikut:
Sekian untuk penjelasan tentang penambahan
program if dan case dari saya.terima kasih
Program Puzzle :
Disini saya akan menjelaskan cara pembuatan program puzzle dengan menggunakan visual basic,dan langkah pembuatannya adalah sebagai berikut:
Langkah pertama membuat 10 button.9 button yang nantinya akan di isi dengan angka 1-9 dan 1 button diisi dengan acak dan stop,kemudian sembilan button tersebut dikelompokan menjadi satu dengan menggunakan groupbox:
Langkah kedua setiap button diisi dengan kode program untuk mengubah susunan puzzle, yaitu dengan mengklik tombol disekitar tombol kosong baik secara vertikal maupum horisontal.untuk kodenya sebagai berikut:
Private Sub bt1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt1.Click
If bt2.Text = "" Then
temp = bt2.Text
bt2.Text = bt1.Text
bt1.Text = temp
ElseIf bt4.Text = "" Then
temp = bt4.Text
bt4.Text = bt1.Text
bt1.Text = temp
End If
End Sub
Private Sub bt2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt2.Click
If bt1.Text = "" Then
temp = bt1.Text
bt1.Text = bt2.Text
bt2.Text = temp
ElseIf bt3.Text = "" Then
temp = bt3.Text
bt3.Text = bt2.Text
bt2.Text = temp
ElseIf bt5.Text = "" Then
temp = bt5.Text
bt5.Text = bt2.Text
bt2.Text = temp
End If
End Sub
Private Sub bt3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt3.Click
If bt2.Text = "" Then
temp = bt2.Text
bt2.Text = bt3.Text
bt3.Text = temp
ElseIf bt6.Text = "" Then
temp = bt6.Text
bt6.Text = bt3.Text
bt3.Text = temp
End If
End Sub
Private Sub bt4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt4.Click
If bt1.Text = "" Then
temp = bt1.Text
bt1.Text = bt4.Text
bt4.Text = temp
ElseIf bt5.Text = "" Then
temp = bt5.Text
bt5.Text = bt4.Text
bt4.Text = temp
ElseIf bt7.Text = "" Then
temp = bt7.Text
bt7.Text = bt4.Text
bt4.Text = temp
End If
End Sub
Private Sub bt5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt5.Click
If bt2.Text = "" Then
temp = bt2.Text
bt2.Text = bt5.Text
bt5.Text = temp
ElseIf bt6.Text = "" Then
temp = bt6.Text
bt6.Text = bt5.Text
bt5.Text = temp
ElseIf bt4.Text = "" Then
temp = bt4.Text
bt4.Text = bt5.Text
bt5.Text = temp
ElseIf bt8.Text = "" Then
temp = bt8.Text
bt8.Text = bt5.Text
bt5.Text = temp
End If
End Sub
Private Sub bt6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt6.Click
If bt3.Text = "" Then
temp = bt3.Text
bt3.Text = bt6.Text
bt6.Text = temp
ElseIf bt5.Text = "" Then
temp = bt5.Text
bt5.Text = bt6.Text
bt6.Text = temp
ElseIf bt9.Text = "" Then
temp = bt9.Text
bt9.Text = bt6.Text
bt6.Text = temp
End If
If bt1.Text = "1" And bt2.Text = "2" And bt3.Text = "3" And bt4.Text = "4" And bt5.Text = "5" And _
bt6.Text = "6" And bt7.Text = "7" And bt8.Text = "8" Then
MsgBox("Selamat Anda Berhasil")
End If
End Sub
Private Sub bt7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt7.Click
If bt4.Text = "" Then
temp = bt4.Text
bt4.Text = bt7.Text
bt7.Text = temp
ElseIf bt8.Text = "" Then
temp = bt8.Text
bt8.Text = bt7.Text
bt7.Text = temp
End If
End Sub
Private Sub bt8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt8.Click
If bt7.Text = "" Then
temp = bt7.Text
bt7.Text = bt8.Text
bt8.Text = temp
ElseIf bt5.Text = "" Then
temp = bt5.Text
bt5.Text = bt8.Text
bt8.Text = temp
ElseIf bt9.Text = "" Then
temp = bt9.Text
bt9.Text = bt8.Text
bt8.Text = temp
End If
If bt1.Text = "1" And bt2.Text = "2" And bt3.Text = "3" And bt4.Text = "4" And bt5.Text = "5" And _
bt6.Text = "6" And bt7.Text = "7" And bt8.Text = "8" Then
MsgBox("Selamat Anda Berhasil")
End If
End Sub
Private Sub bt9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt9.Click
If bt6.Text = "" Then
temp = bt6.Text
bt6.Text = bt9.Text
bt9.Text = temp
ElseIf bt8.Text = "" Then
temp = bt8.Text
bt8.Text = bt9.Text
bt9.Text = temp
End If
If bt1.Text = "1" And bt2.Text = "2" And bt3.Text = "3" And bt4.Text = "4" And bt5.Text = "5" And _
bt6.Text = "6" And bt7.Text = "7" And bt8.Text = "8" Then
MsgBox("Selamat Anda Berhasil")
End If
End Sub
If bt2.Text = "" Then
temp = bt2.Text
bt2.Text = bt1.Text
bt1.Text = temp
ElseIf bt4.Text = "" Then
temp = bt4.Text
bt4.Text = bt1.Text
bt1.Text = temp
End If
End Sub
Private Sub bt2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt2.Click
If bt1.Text = "" Then
temp = bt1.Text
bt1.Text = bt2.Text
bt2.Text = temp
ElseIf bt3.Text = "" Then
temp = bt3.Text
bt3.Text = bt2.Text
bt2.Text = temp
ElseIf bt5.Text = "" Then
temp = bt5.Text
bt5.Text = bt2.Text
bt2.Text = temp
End If
End Sub
Private Sub bt3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt3.Click
If bt2.Text = "" Then
temp = bt2.Text
bt2.Text = bt3.Text
bt3.Text = temp
ElseIf bt6.Text = "" Then
temp = bt6.Text
bt6.Text = bt3.Text
bt3.Text = temp
End If
End Sub
Private Sub bt4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt4.Click
If bt1.Text = "" Then
temp = bt1.Text
bt1.Text = bt4.Text
bt4.Text = temp
ElseIf bt5.Text = "" Then
temp = bt5.Text
bt5.Text = bt4.Text
bt4.Text = temp
ElseIf bt7.Text = "" Then
temp = bt7.Text
bt7.Text = bt4.Text
bt4.Text = temp
End If
End Sub
Private Sub bt5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt5.Click
If bt2.Text = "" Then
temp = bt2.Text
bt2.Text = bt5.Text
bt5.Text = temp
ElseIf bt6.Text = "" Then
temp = bt6.Text
bt6.Text = bt5.Text
bt5.Text = temp
ElseIf bt4.Text = "" Then
temp = bt4.Text
bt4.Text = bt5.Text
bt5.Text = temp
ElseIf bt8.Text = "" Then
temp = bt8.Text
bt8.Text = bt5.Text
bt5.Text = temp
End If
End Sub
Private Sub bt6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt6.Click
If bt3.Text = "" Then
temp = bt3.Text
bt3.Text = bt6.Text
bt6.Text = temp
ElseIf bt5.Text = "" Then
temp = bt5.Text
bt5.Text = bt6.Text
bt6.Text = temp
ElseIf bt9.Text = "" Then
temp = bt9.Text
bt9.Text = bt6.Text
bt6.Text = temp
End If
If bt1.Text = "1" And bt2.Text = "2" And bt3.Text = "3" And bt4.Text = "4" And bt5.Text = "5" And _
bt6.Text = "6" And bt7.Text = "7" And bt8.Text = "8" Then
MsgBox("Selamat Anda Berhasil")
End If
End Sub
Private Sub bt7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt7.Click
If bt4.Text = "" Then
temp = bt4.Text
bt4.Text = bt7.Text
bt7.Text = temp
ElseIf bt8.Text = "" Then
temp = bt8.Text
bt8.Text = bt7.Text
bt7.Text = temp
End If
End Sub
Private Sub bt8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt8.Click
If bt7.Text = "" Then
temp = bt7.Text
bt7.Text = bt8.Text
bt8.Text = temp
ElseIf bt5.Text = "" Then
temp = bt5.Text
bt5.Text = bt8.Text
bt8.Text = temp
ElseIf bt9.Text = "" Then
temp = bt9.Text
bt9.Text = bt8.Text
bt8.Text = temp
End If
If bt1.Text = "1" And bt2.Text = "2" And bt3.Text = "3" And bt4.Text = "4" And bt5.Text = "5" And _
bt6.Text = "6" And bt7.Text = "7" And bt8.Text = "8" Then
MsgBox("Selamat Anda Berhasil")
End If
End Sub
Private Sub bt9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bt9.Click
If bt6.Text = "" Then
temp = bt6.Text
bt6.Text = bt9.Text
bt9.Text = temp
ElseIf bt8.Text = "" Then
temp = bt8.Text
bt8.Text = bt9.Text
bt9.Text = temp
End If
If bt1.Text = "1" And bt2.Text = "2" And bt3.Text = "3" And bt4.Text = "4" And bt5.Text = "5" And _
bt6.Text = "6" And bt7.Text = "7" And bt8.Text = "8" Then
MsgBox("Selamat Anda Berhasil")
End If
End Sub
Kemudian untuk mengacak susunan angka pada tombol button saat program dijalankan,caranya adalah sebagai berikut:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Visible = False
Timer1.Enabled = False
Dim arrangka(0 To 8) As Integer
Dim angka, i, x, y As Integer
Dim bt As Windows.Forms.Control
For x = 0 To 8
Ulang:
Randomize()
angka = Int((9 * Rnd()) + 1)
For y = 0 To 8
If angka = arrangka(y) Then
GoTo Ulang
End If
Next y
arrangka(x) = angka
Next x
For i = 0 To 8
ListBox1.Items.Add(arrangka(i))
Next
bt1.Text = ListBox1.Items(0)
bt2.Text = ListBox1.Items(1)
bt3.Text = ListBox1.Items(2)
bt4.Text = ListBox1.Items(3)
bt5.Text = ListBox1.Items(4)
bt6.Text = ListBox1.Items(5)
bt7.Text = ListBox1.Items(6)
bt8.Text = ListBox1.Items(7)
bt9.Text = ListBox1.Items(8)
For Each bt In Me.GroupBox1.Controls
If bt.Text = 9 Then
bt.Text = ""
End If
Next
ListBox1.Items.Clear()
End Sub
ListBox1.Visible = False
Timer1.Enabled = False
Dim arrangka(0 To 8) As Integer
Dim angka, i, x, y As Integer
Dim bt As Windows.Forms.Control
For x = 0 To 8
Ulang:
Randomize()
angka = Int((9 * Rnd()) + 1)
For y = 0 To 8
If angka = arrangka(y) Then
GoTo Ulang
End If
Next y
arrangka(x) = angka
Next x
For i = 0 To 8
ListBox1.Items.Add(arrangka(i))
Next
bt1.Text = ListBox1.Items(0)
bt2.Text = ListBox1.Items(1)
bt3.Text = ListBox1.Items(2)
bt4.Text = ListBox1.Items(3)
bt5.Text = ListBox1.Items(4)
bt6.Text = ListBox1.Items(5)
bt7.Text = ListBox1.Items(6)
bt8.Text = ListBox1.Items(7)
bt9.Text = ListBox1.Items(8)
For Each bt In Me.GroupBox1.Controls
If bt.Text = 9 Then
bt.Text = ""
End If
Next
ListBox1.Items.Clear()
End Sub
Langkah selanjutnya untuk mengacak susunan tombol, yaitu dengan menekan tombol ACAK,maka susunan tombol akan terus menerus mengacak sampai tombol ACAK yang sudah berubah properti textnya menjadi BERHENTI lalu ditekan atau diklik kembali maka tombol sedang mengacak akan berhenti.Untuk kode programnya adalah sebagai berikut:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim arrangka(0 To 8) As Integer
Dim angka, i, x, y As Integer
Dim bt As Windows.Forms.Control
For x = 0 To 8
Ulang:
Randomize()
angka = Int((9 * Rnd()) + 1)
For y = 0 To 8
If angka = arrangka(y) Then
GoTo Ulang
End If
Next y
arrangka(x) = angka
Next x
For i = 0 To 8
ListBox1.Items.Add(arrangka(i))
Next
bt1.Text = ListBox1.Items(0)
bt2.Text = ListBox1.Items(1)
bt3.Text = ListBox1.Items(2)
bt4.Text = ListBox1.Items(3)
bt5.Text = ListBox1.Items(4)
bt6.Text = ListBox1.Items(5)
bt7.Text = ListBox1.Items(6)
bt8.Text = ListBox1.Items(7)
bt9.Text = ListBox1.Items(8)
For Each bt In Me.GroupBox1.Controls
If bt.Text = 9 Then
bt.Text = ""
End If
Next
ListBox1.Items.Clear()
End Sub
Private Sub btacak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btacak.Click
If Timer1.Enabled = True Then
While Timer1.Enabled = True
btacak.Text = "ACAK"
Timer1.Enabled = False
End While
Else
While Timer1.Enabled = False
btacak.Text = "BERHENTI"
Timer1.Enabled = True
End While
End If
End Sub
Dim arrangka(0 To 8) As Integer
Dim angka, i, x, y As Integer
Dim bt As Windows.Forms.Control
For x = 0 To 8
Ulang:
Randomize()
angka = Int((9 * Rnd()) + 1)
For y = 0 To 8
If angka = arrangka(y) Then
GoTo Ulang
End If
Next y
arrangka(x) = angka
Next x
For i = 0 To 8
ListBox1.Items.Add(arrangka(i))
Next
bt1.Text = ListBox1.Items(0)
bt2.Text = ListBox1.Items(1)
bt3.Text = ListBox1.Items(2)
bt4.Text = ListBox1.Items(3)
bt5.Text = ListBox1.Items(4)
bt6.Text = ListBox1.Items(5)
bt7.Text = ListBox1.Items(6)
bt8.Text = ListBox1.Items(7)
bt9.Text = ListBox1.Items(8)
For Each bt In Me.GroupBox1.Controls
If bt.Text = 9 Then
bt.Text = ""
End If
Next
ListBox1.Items.Clear()
End Sub
Private Sub btacak_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btacak.Click
If Timer1.Enabled = True Then
While Timer1.Enabled = True
btacak.Text = "ACAK"
Timer1.Enabled = False
End While
Else
While Timer1.Enabled = False
btacak.Text = "BERHENTI"
Timer1.Enabled = True
End While
End If
End Sub
Sekian untuk penjelasan dari program puzzle dari saya.
Tidak ada komentar:
Posting Komentar