Minggu, 13 Maret 2011

Membuat Tombol yang berpindah pindah

Kita akan berlatih membuat tombol yang berpindah-pindah..
Misal ada 6 kotakan... kotak 1 sampai lima ada tombolnya yang telah diwarnai... sedang kotak yang ke enam kosong... ketika salah satu dari tombol itu diklik maka tombol akan menempati posisi yang kosong...
misal... tombol satu diklik... maka tombol satu itu akan menempati posisi di kotak no enam, lalu klo tombol kedua diklik maka tombol 2 akan menempati posisi kotak no satu yang kosong.. begitu seterusnya..
Lihat gambar....

Listing programnya:
Public Class Form1
    Dim vButton As System.Windows.Forms.Button
    'buat variabel general utk menyimpan tombol yang invisible, misal vButton
'buat procedure aktif untuk memindah tulisan dan warna tombol ke lokasi yang kosong(tombol yang invisible)

Sub aktif(ByVal b As System.Windows.Forms.Button)
        vButton.Visible = True
        vButton.Text = b.Text
        vButton.BackColor = b.BackColor
        b.Visible = False
    End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button6.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click
        Call aktif(sender)
        vButton = sender
'panggil procedure aktif
'ganti variable vButton
        
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        vButton = Button6
'setting awal tombol 6 di invisible
    End Sub
End Class

cara lain yang lebih panjang tapi secara logika lebih mudah dimengerti:

Public Class Form1
   
    

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       'cara 1 masukkan kode ini di setiap tombol, dan ganti tombol yang visible dan invisible
'warna tolbol belum dimasukkan

        'If Button2.Visible = False Then
        '    Button2.Visible = True
        '    Button2.Text = Button1.Text
        '    Button1.Visible = False
        'ElseIf Button3.Visible = False Then
        '    Button3.Visible = True
        '    Button3.Text = Button1.Text
        '    Button1.Visible = False
        'ElseIf Button4.Visible = False Then
        '    Button4.Visible = True
        '    Button4.Text = Button1.Text
        '    Button1.Visible = False
        'ElseIf Button5.Visible = False Then
        '    Button5.Visible = True
        '    Button5.Text = Button1.Text
        '    Button1.Visible = False
        'ElseIf Button6.Visible = False Then
        '    Button6.Visible = True
        '    Button6.Text = Button1.Text
        '    Button1.Visible = False
        'End If
        '===========================sampai sini cara panjang 1;
        'cara 2
        '===========================
        Call aktif(Button1)
        'panggil prosedur aktif, buat disemua tombol seperti ini

    End Sub

'buat prosedure aktif
Sub aktif(ByVal b As System.Windows.Forms.Button)


        If Button1.Visible = False Then
            Button1.Visible = True
            Button1.Text = b.Text
            Button1.BackColor = b.BackColor
            b.Visible = False
        ElseIf Button2.Visible = False Then
            Button2.Visible = True
            Button2.Text = b.Text
            Button2.BackColor = b.BackColor
            b.Visible = False
        ElseIf Button3.Visible = False Then
            Button3.Visible = True
            Button3.Text = b.Text
            Button3.BackColor = b.BackColor
            b.Visible = False
        ElseIf Button4.Visible = False Then
            Button4.Visible = True
            Button4.Text = b.Text
            Button4.BackColor = b.BackColor
            b.Visible = False
        ElseIf Button5.Visible = False Then
            Button5.Visible = True
            Button5.Text = b.Text
            Button5.BackColor = b.BackColor
            b.Visible = False
        ElseIf Button6.Visible = False Then
            Button6.Visible = True
            Button6.Text = b.Text
            Button6.BackColor = b.BackColor
            b.Visible = False
        End If

    End Sub

    '==================== sampai sini cara 2
End Class




Tidak ada komentar:

Posting Komentar