Konu 6: Open File Dialog, Save File Dialog ve Not Defteri Yapımı

Ö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
        OfD1.Title = "metin dosyası aç"
        OfD1.Filter = "metin dosyaları (*.txt)|*.txt|resim dosyaları (*.jpg)|*.jpg|Tüm dosyalar (*.*)|*.*"
        OfD1.FilterIndex = 1
        OfD1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
        'OfD1.ShowDialog()
        If OfD1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim yol As String
            yol = OfD1.FileName
            TextBox1.Text = My.Computer.FileSystem.ReadAllText(yol)
        End If
    End Sub
    Private Sub kaydet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles kaydet.Click
        sfd1.Title = "metin dosyası kaydet"
        sfd1.Filter = "metin dosyaları (*.txt)|*.txt|resim dosyaları (*.jpg)|*.jpg|Tüm dosyalar (*.*)|*.*"
        sfd1.FilterIndex = 1
        sfd1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.Desktop
        'OfD1.ShowDialog()
        If sfd1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim yol As String
            yol = sfd1.FileName
            My.Computer.FileSystem.WriteAllText(yol, TextBox1.Text, False)
        End If
    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 5: Msgbox, Messagebox, Form closing, Inputbox

Ö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
        'MsgBox("merhaba")
        Dim cevap As MsgBoxResult

        cevap = MsgBox("merhaba", MsgBoxStyle.OkCancel, "soru")
        If cevap = MsgBoxResult.Ok Then
            MsgBox("okeye basıldı")
        Else
            MsgBox("kensıla basıldı")
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim cevap As DialogResult

        cevap = MessageBox.Show("merhaba", "soru", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly, False)
        If cevap = Windows.Forms.DialogResult.Yes Then
            MsgBox("evet")
        ElseIf cevap = Windows.Forms.DialogResult.No Then
            MsgBox("hayır")
        Else
            MsgBox("iptal")
        End If

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Dim cevap As MsgBoxResult
        cevap = MsgBox("kapatmak istediğinizden emin misiniz?", MsgBoxStyle.YesNo, "Kapatma işlemi")
        If cevap = MsgBoxResult.No Then
            e.Cancel = True
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim adi As String
        adi = InputBox("isminizi giriniz", "isim girişi", "ahmet")
        MsgBox(adi)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim yas As Integer
        yas = Val(InputBox("yaşınızı giriniz", "yaş girişi", 18))
        MsgBox(yas)
    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 4: Mdi Formlar

Ö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
        Form2.MdiParent = Me
        Form2.Show()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim frm As New Form
        frm.MdiParent = Me
        frm.Show()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.ActiveMdiChild.Close()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.ActiveMdiChild.Text = "merhaba"
        Dim btn As New Button
        Me.ActiveMdiChild.Controls.Add(btn)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        'Me.LayoutMdi(MdiLayout.ArrangeIcons)
        'Me.LayoutMdi(MdiLayout.Cascade)
        'Me.LayoutMdi(MdiLayout.TileHorizontal)
        Me.LayoutMdi(MdiLayout.TileVertical)
    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 3: Formlar Ve Form İşlemleri

Ö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
        'form2 gösterilir
        Form2.Show()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'yeni form2ler gosterilir
        Dim frm As New Form2
        frm.Show()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'yeni form olusturulur
        Dim frm As New Form
        frm.Show()
        'yeni buton olusturulur
        Dim btn As New Button
        'butona olay atanir
        AddHandler btn.Click, AddressOf islem
        'yeni buton, yeni form üzerinde gosterilir
        frm.Controls.Add(btn)
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        'form2 gizlenir
        Form2.Hide()
    End Sub
    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        'form2yi kapatır
        Form2.Close()
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        MsgBox("merhaba")
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        'form2 dialog penceresi seklinde gosterilir
        Form2.ShowDialog()
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        'yeni buton olusturulur
        Dim btn As New Button
        'butona olay atanir
        AddHandler btn.Click, AddressOf islem
        'olusturulan buton form2ye eklenir
        Form2.Controls.Add(btn)
    End Sub
    Private Sub islem(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'islem isimli olay AddHandler btn.Click, AddressOf islem kodlari ile
        'butonlara atanır
        MsgBox("bu yeni butonun mesajı")
    End Sub

End Class

Konu 2: Timer Kontrollü Solucan Örneği

Örnek Uygulama

Ekran Görüntüsü

Public Class Form1
    Dim skor As Integer = 1
    Dim yon As String

    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Up Then
            yon = "yukarı"
        ElseIf e.KeyCode = Keys.Down Then
            yon = "aşağı"
        ElseIf e.KeyCode = Keys.Left Then
            yon = "sol"
        ElseIf e.KeyCode = Keys.Right Then
            yon = "sağ"
        End If

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If yon = "yukarı" Then
            If solucan.Top < 0 Then
                solucan.Top = Me.Height - 40
            End If
            solucan.Top -= 5
        ElseIf yon = "aşağı" Then
            If solucan.Top > Me.Height - 45 Then
                solucan.Top = -5
            End If
            solucan.Top += 5
        ElseIf yon = "sol" Then
            If solucan.Left < 5 Then
                solucan.Left = Me.Width - 15
            End If
            solucan.Left -= 5
        ElseIf yon = "sağ" 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)
                If skor Mod 3 = 0 Then
                    If Timer1.Interval > 10 Then
                        Timer1.Interval -= 10
                    End If
                End If
            End If

        End If

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Start()
    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