Konu 38: Ek Anlatım (Nesne Oluşturma, Olay Atama, Diziler)

Örnek Uygulama

Ekran Görüntüsü

Public Class Form1
    Dim butonlar(2, 2) As Button
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For j As Integer = 0 To 2
            For i As Integer = 0 To 2
                Dim btn As New Button
                btn.Width = 60
                btn.Height = 30
                btn.Left = 65 * i + 10
                btn.Top = 100 + 30 * j
                btn.Text = "merhaba"
                btn.Name = j & i
                Me.Controls.Add(btn)
                AddHandler btn.Click, AddressOf btnkilik
                butonlar(j, i) = btn
            Next
        Next
    End Sub

    Private Sub btnkilik(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim isim As String
        isim = sender.name

        For j As Integer = 0 To 2
            For i As Integer = 0 To 2
                If butonlar(j, i).Name <> isim And (i = 1 Or j = 1) Then
                    butonlar(j, i).BackColor = Color.FromArgb(255, Rnd() * 255, Rnd() * 255, Rnd() * 255)
                End If
            Next
        Next
    End Sub

End Class

Konu 37: Eş bulma oyunu örneği

Örnek Uygulama

Ekran Görüntüsü

   

Public Class Form1
    Dim btnler(3, 3) As Button
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        tahtaolustur()
        oyunubaslat()
    End Sub

    Dim kactane As Integer
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        kactane = ((UBound(btnler, 1) + 1) * (UBound(btnler, 2) + 1)) / 2
        For i As Integer = 1 To kactane
            For j As Integer = 1 To 2
                Dim x, y As Integer

                Do
                    x = Rnd() * UBound(btnler, 2)
                    y = Rnd() * UBound(btnler, 1)
                Loop Until btnler(y, x).Name = ""

                btnler(y, x).Name = i

            Next
        Next
    End Sub
    Dim onceki As Button
    Dim sayac As Integer = 0
    Private Sub olay(ByVal sender As System.Object, ByVal e As System.EventArgs)
        sender.Text = sender.Name
        If IsNothing(onceki) Then
            onceki = sender
        Else
            'ilk butonla eslesiyor mu?
            If onceki.Name = sender.Name Then
                sayac += 1

                onceki.Enabled = False
                sender.Enabled = False
                onceki.BackColor = Color.Aqua
                sender.BackColor = Color.Aqua
                If sayac = kactane Then
                    seviyeartir()
                End If
            Else
                Me.Enabled = False
                Application.DoEvents()
                Threading.Thread.Sleep(1000)
                onceki.Text = ""
                sender.Text = ""
                Me.Enabled = True
            End If

            onceki = Nothing
        End If
    End Sub

    Sub tahtaolustur()
        For i As Integer = 0 To UBound(btnler, 1)
            For j As Integer = 0 To UBound(btnler, 2)
                Dim btn As New Button
                Me.Controls.Add(btn)
                btn.Top = 50 * i + 50
                btn.Left = 50 * j + 10
                btn.Width = 45
                btn.Height = 45
                btnler(i, j) = btn
                AddHandler btn.Click, AddressOf olay
            Next
        Next
        Me.Width = UBound(btnler, 2) * 50 + 70
        Me.Height = UBound(btnler, 1) * 50 + 150
    End Sub

    Sub seviyeartir()
        MsgBox("aferin")
        sayac = 0
        For i As Integer = 0 To UBound(btnler, 1)
            For j As Integer = 0 To UBound(btnler, 2)
                btnler(i, j).Dispose()
            Next
        Next

        ReDim btnler(Math.Sqrt(kactane * 2) + 1, Math.Sqrt(kactane * 2) + 1)
        tahtaolustur()
        oyunubaslat()

    End Sub

    Sub oyunubaslat()
        kactane = ((UBound(btnler, 1) + 1) * (UBound(btnler, 2) + 1)) / 2
        For i As Integer = 1 To kactane
            For j As Integer = 1 To 2
                Dim x, y As Integer
git:
                x = Rnd() * UBound(btnler, 2)
                y = Rnd() * UBound(btnler, 1)
                If btnler(y, x).Name = "" Then
                    btnler(y, x).Name = i
                Else
                    GoTo git
                End If

            Next
        Next
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Randomize()
    End Sub
End Class

 

Konu 36: Örnek(Yılan Oyunu)

Örnek Uygulama

Ekran Görüntüsü

Public Class Form1
    Dim tus As Keys
    Dim skor As Integer
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        tus = e.KeyCode

    End Sub
    Dim solucan, yem As New Label
    Dim dizi(0) As Label
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        solucan.Width = 10
        solucan.Height = 10
        solucan.Top = Rnd() * (Me.Height - 40)
        solucan.Left = Rnd() * (Me.Width - 20)
        solucan.BackColor = Color.Blue

        dizi(0) = solucan

        yem.Width = 10
        yem.Height = 10
        yem.Top = Rnd() * (Me.Height - 40)
        yem.Left = Rnd() * (Me.Width - 20)
        yem.BackColor = Color.Red

        Me.Controls.Add(solucan)
        Me.Controls.Add(yem)
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        'dizinin ilk elemanı hariç her bir elemanı, bir oncekinin yerine gecer
        For i As Integer = UBound(dizi) To 1 Step -1
            dizi(i).Top = dizi(i - 1).Top
            dizi(i).Left = dizi(i - 1).Left
        Next
        'basılan tuşa göre ilk label hareket eder
        If tus = Keys.Up Then
            If solucan.Top < 0 Then
                solucan.Top = Me.Height - 40
            End If
            solucan.Top -= 10
        ElseIf tus = Keys.Down Then
            If solucan.Top > Me.Height - 45 Then
                solucan.Top = -5
            End If
            solucan.Top += 10
        ElseIf tus = Keys.Left Then
            If solucan.Left < 5 Then
                solucan.Left = Me.Width - 15
            End If
            solucan.Left -= 10
        ElseIf tus = Keys.Right Then
            If solucan.Left > Me.Width - 20 Then
                solucan.Left = -5
            End If
            solucan.Left += 10
        End If

        'yem yenmişmi
        If (solucan.Left > yem.Left - 10 And solucan.Left < yem.Left + 10) Then
            If (solucan.Top > yem.Top - 10 And solucan.Top < yem.Top + 10) Then
                'yem yenmiş
                'halka oluşturulur
                skor += 1
                If skor Mod 2 = 0 Then
                    Timer1.Interval -= 100
                End If
                Label1.Text = skor
                yem.Top = Rnd() * (Me.Height - 40)
                yem.Left = Rnd() * (Me.Width - 20)
                ReDim Preserve dizi(skor)
                Dim halka As New Label
                halka.BackColor = Color.FromArgb(255, Rnd() * 255, Rnd() * 255, Rnd() * 255)
                halka.Width = 10
                halka.Height = 10
                Me.Controls.Add(halka)
                dizi(skor) = halka
                If skor > 1 Then
                    'yilan iki den fazla halkaya sahipse
                    'en son eklenen halka, ondan önceki iki halkanın konumuna göre yerleştirilir
                    If dizi(skor - 1).Top = dizi(skor - 2).Top Then
                        If dizi(skor - 1).Left > dizi(skor - 2).Left Then
                            halka.Left = dizi(skor - 1).Left + 10
                            halka.Top = dizi(skor - 1).Top
                        ElseIf dizi(skor - 1).Left < dizi(skor - 2).Left Then
                            halka.Left = dizi(skor - 1).Left - 10
                            halka.Top = dizi(skor - 1).Top
                        End If
                    ElseIf dizi(skor - 1).Left = dizi(skor - 2).Left Then
                        If dizi(skor - 1).Top > dizi(skor - 2).Top Then
                            halka.Top = dizi(skor - 1).Top + 10
                            halka.Left = dizi(skor - 1).Left
                        ElseIf dizi(skor - 1).Top < dizi(skor - 2).Top Then
                            halka.Top = dizi(skor - 1).Top - 10
                            halka.Left = dizi(skor - 1).Left
                        End If
                    End If
                Else
                    'yilanin maksimum 2 halkası varsa 
                    'basılan yön tuşuna göre son halkanın yönü belirlenir
                    If tus = Keys.Up Then
                        halka.Top = dizi(skor - 1).Top + 10
                        halka.Left = dizi(skor - 1).Left
                    ElseIf tus = Keys.Down Then
                        halka.Top = dizi(skor - 1).Top - 10
                        halka.Left = dizi(skor - 1).Left
                    ElseIf tus = Keys.Left Then
                        halka.Left = dizi(skor - 1).Left + 10
                        halka.Top = dizi(skor - 1).Top
                    ElseIf tus = Keys.Right Then
                        halka.Left = dizi(skor - 1).Left - 10
                        halka.Top = dizi(skor - 1).Top
                    End If
                End If
            End If
        End If

        'yılan kendine çarparsa oyun yeniden başlatılır
        For i As Integer = 0 To UBound(dizi) - 1
            For j As Integer = i + 1 To UBound(dizi)
                If dizi(i).Top = dizi(j).Top And dizi(i).Left = dizi(j).Left Then
                    yandi()
                    Exi t Sub
                End If
            Next
        Next

    End Sub

    Sub yandi()
        MsgBox("yandı")
        skor = 0
        For k As Integer = 1 To UBound(dizi)
            dizi(k).Dispose()
        Next
        ReDim Preserve dizi(0)
    End Sub
End Class

Konu 35: Örnek(Solucan Çoklu Yem Örneği)

Örnek Uygulama

Ekran Görüntüsü

Public Class Form1
    Dim skor As Integer
    Dim yemler(0) As Label
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Up Then
            If solucan.Top < 0 Then
                solucan.Top = Me.Height - 40
            End If
            solucan.Top -= 5
        ElseIf e.KeyCode = Keys.Down Then
            If solucan.Top > Me.Height - 45 Then
                solucan.Top = -5
            End If
            solucan.Top += 5
        ElseIf e.KeyCode = Keys.Left Then
            If solucan.Left < 0 Then
                solucan.Left = Me.Width - 15
            End If
            solucan.Left -= 5
        ElseIf e.KeyCode = Keys.Right Then
            If solucan.Left > Me.Width - 15 Then
                solucan.Left = -5
            End If
            solucan.Left += 5
        End If

        For i As Integer = 0 To UBound(yemler)
            If (solucan.Left > yemler(i).Left - 10 And solucan.Left < yemler(i).Left + 10) And (solucan.Top > yemler(i).Top - 10 And solucan.Top < yemler(i).Top + 10) Then
                skor += 1
                Label1.Text = skor
                ReDim Preserve yemler(skor)
                Dim yeni As New Label
                yeni.Width = 10
                yeni.Height = 10
                yeni.Top = Rnd() * (Me.Height - 40)
                yeni.Left = Rnd() * (Me.Width - 15)
                yeni.BackColor = Color.FromArgb(255, Rnd() * 255, Rnd() * 255, Rnd() * 255)
                Me.Controls.Add(yeni)
                yemler(UBound(yemler)) = yeni

                yemler(i).Top = Rnd() * (Me.Height - 40)
                yemler(i).Left = Rnd() * (Me.Width - 15)
            End If
        Next

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        yemler(0) = yem
    End Sub
End Class

Konu 34: Solucan Oyunu, Nesne Oluşturma, Form Olayları

Örnek Uygulama

Ekran Görüntüsü

Public Class Form1
    Dim skor As Integer
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Up Then
            If solucan.Top < 0 Then
                solucan.Top = Me.Height - 40
            End If
            solucan.Top -= 5
        ElseIf e.KeyCode = Keys.Down Then
            If solucan.Top > Me.Height - 45 Then
                solucan.Top = -5
            End If
            solucan.Top += 5
        ElseIf e.KeyCode = Keys.Left Then
            If solucan.Left < 5 Then
                solucan.Left = Me.Width - 15
            End If
            solucan.Left -= 5
        ElseIf e.KeyCode = Keys.Right Then
            If solucan.Left > Me.Width - 20 Then
                solucan.Left = -5
            End If
            solucan.Left += 5
        End If
        'kontrol et
        If (solucan.Left > yem.Left - 10 And solucan.Left < yem.Left + 10) Then
            If (solucan.Top > yem.Top - 10 And solucan.Top < yem.Top + 10) Then
                'baska yerde çıksın
                skor += 1
                Label1.Text = skor
                yem.Top = Rnd() * (Me.Height - 40)
                yem.Left = Rnd() * (Me.Width - 20)
            End If
        End If
    End Sub
End Class

Konu 33: Diziler, Nesne Oluşturma, Olaylar, Fonksiyon, Yordam

Örnek Uygulama

Ekran Görüntüsü

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
        Dim dizi(6) As Integer
        For i As Integer = 0 To UBound(dizi)
            dizi(i) = i + 1
            ListBox1.Items.Add(dizi(i))
        Next
        'MsgBox("diziye elemanlar atandı")
        For j As Integer = 0 To 2
            Dim rast As Integer = Rnd() * UBound(dizi)
            Label1.Text = dizi(rast)
            For i As Integer = rast + 1 To UBound(dizi)
                dizi(i - 1) = dizi(i)
            Next
            Dim x As Integer = UBound(dizi) - 1
            ReDim Preserve dizi(x)
        Next
        For i As Integer = 0 To UBound(dizi)
            ListBox2.Items.Add(dizi(i))
        Next
    End Sub

    Dim dizi(2, 3) As String
    Dim butonlar(2, 3) As Button
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim sayac As Integer = 1
        For j As Integer = 0 To UBound(dizi, 1)
            For i As Integer = 0 To UBound(dizi, 2)
                dizi(j, i) = sayac
                Dim lbl As New Button
                lbl.Name = dizi(j, i)
                lbl.Width = 30
                lbl.Height = 20
                lbl.Left = i * 35 + 300
                lbl.Top = j * 25 + 100
                lbl.BackColor = Color.Red
                AddHandler lbl.Click, AddressOf butonl
                AddHandler lbl.MouseMove, AddressOf gezin
                Me.Controls.Add(lbl)
                butonlar(j, i) = lbl
                sayac += 1

            Next
        Next
    End Sub

    Dim ilkmi As Boolean = True
    Dim sayi1, sayi2 As Integer
    Private Sub butonl(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If ilkmi = True Then
            sayi1 = sender.text
            ilkmi = False
            Label2.Text = "sayi1"
        Else
            sayi2 = sender.text
            ilkmi = True
            Label2.Text = "sayi2"
            Dim toplam As Integer = sayi1 + sayi2

            karsilastir(toplam)

        End If

    End Sub

    Sub karsilastir(ByVal sayi As Integer)
        For j As Integer = 0 To UBound(butonlar, 1)
            For i As Integer = 0 To UBound(butonlar, 2)
                If butonlar(j, i).Text = sayi Then
                    butonlar(j, i).BackColor = Color.Red
                Else
                    butonlar(j, i).BackColor = Color.White
                End If
            Next
        Next
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        topla(5, 2)
    End Sub

    Sub topla(ByVal a As Integer, ByVal b As Integer)
        Label3.Text = a + b
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Label3.Text = topla2(3, 2)
    End Sub

    Function topla2(ByVal a As Integer, ByVal b As Integer) As Integer
        topla2 = a + b
    End Function

    Private Sub gezin(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

        For j As Integer = 0 To UBound(butonlar, 1)
            For i As Integer = 0 To UBound(butonlar, 2)
                butonlar(j, i).BackColor = Color.Red
                butonlar(j, i).Text = ""
            Next
        Next
        sender.BackColor = Color.White
        sender.text = sender.name
    End Sub
End Class

Konu 32: Form Üzerinde Mouse Olayı Ve Nesne Oluşturma, Oluşturulan Nesnenin Yok Edilmesi

Örnek Uygulama

Ekran Görüntüsü

Public Class Form1
    Dim olusturmu As Boolean
    Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick

        Dim lbl As New Label
        lbl.Text = " "
        lbl.Width = 10
        lbl.Height = 10
        lbl.BackColor = Color.FromArgb(255, Rnd() * 255, Rnd() * 255, Rnd() * 255)
        AddHandler lbl.Click, AddressOf lab
        AddHandler lbl.MouseMove, AddressOf lblmove
        Me.Controls.Add(lbl)
        lbl.Top = e.Y - 5
        lbl.Left = e.X - 5
    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        'Dim lbl As New Label
        'lbl.Text = " "
        'lbl.Width = 10
        'lbl.Height = 10
        'lbl.BackColor = Color.FromArgb(255, Rnd() * 255, Rnd() * 255, Rnd() * 255)
        'Me.Controls.Add(lbl)
        'lbl.Top = e.Y - 5
        'lbl.Left = e.X - 5
    End Sub
    Private Sub lab(ByVal sender As System.Object, ByVal e As System.EventArgs)
        sender.dispose()
    End Sub

    Private Sub lblmove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        sender.dispose()
    End Sub
End Class

Konu 31: Form Üzerinde Mouse Ve Klavye Olayları, Rastgele Renk

Örnek Uygulama

Ekran Görüntüsü

Public Class Form1

    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
        'Label1.Text = "click gerceklesti"
        'Label1.Text
    End Sub

    Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        Label5.Text = e.KeyChar

    End Sub

    Private Sub Form1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        Label3.Text = e.X
        Label4.Text = e.Y
        Label7.Top = e.Y - 5
        Label7.Left = e.X - 20
        Label7.BackColor = Color.FromArgb(255, Rnd() * 255, Rnd() * 255, Rnd() * 255)

    End Sub

    Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
        Label1.Text = e.X
        Label2.Text = e.Y
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Up Then
            Label6.Top -= 5
        ElseIf e.KeyCode = Keys.Down Then
            Label6.Top += 5
        ElseIf e.KeyCode = Keys.Left Then
            Label6.Left -= 5
        ElseIf e.KeyCode = Keys.Right Then
            Label6.Left += 5
        End If
        Label6.BackColor = Color.FromArgb(255, Rnd() * 255, Rnd() * 255, Rnd() * 255)
    End Sub
End Class

Konu 30: İki Boyutlu Diziye Buton Oluşturma, Olay Atama

Örnek Uygulama

Ekran Görüntüsü

Public Class Form1
    Dim butonlar(2, 3) As Button
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sayac As Integer = 1
        For i As Integer = 0 To UBound(butonlar, 1)
            For j As Integer = 0 To UBound(butonlar, 2)
                Dim btn As New Button
                btn.Width = 40
                btn.Height = 40
                btn.Top = 45 * i + 50
                btn.Left = 45 * j + 50
                btn.Text = sayac
                Me.Controls.Add(btn)
                butonlar(i, j) = btn
                sayac += 1
                AddHandler btn.Click, AddressOf islem
            Next
        Next
    End Sub
    Private Sub islem(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MsgBox(sender.text)
    End Sub
End Class

Konu 29: Form Kontrolleri Arasında Gezinme, Buton Oluşturma, Butona Olay Atama

Örnek Uygulama

Ekran Görüntüsü

Public Class Form1
    Dim butonlar(4) As Button
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For i As Integer = 0 To 4
            Dim btn As New Button
            btn.Text = i
            btn.Width = 30
            btn.Top = 50
            btn.Left = 40 * i + 40
            Me.Controls.Add(btn)
            butonlar(i) = btn
            AddHandler btn.Click, AddressOf islem
        Next

    End Sub

    Private Sub islem(ByVal sender As System.Object, ByVal e As System.EventArgs)
        For i As Integer = 0 To UBound(butonlar)
            butonlar(i).BackColor = Color.Blue
            butonlar(i).Enabled = True
        Next
        sender.BackColor = Color.Red
        sender.Enabled = False
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        For Each ctr As Control In Me.Controls
            ctr.BackColor = Color.Green

        Next
    End Sub
End Class