VB.NET'te ListBox1 olaylarını bir düzene koyamadım

Vatansever

Asistan
Katılım
23 Ağustos 2007
Mesajlar
405
Reaksiyon puanı
1
Puanları
18
Arkadaşlar VB.NET'te bir listbox problemim var kodların hepsi var problem if komutuyla bunları doğru şekilde yerleştirmeyi bir türlü yapamıyorum istediğim çok basit
1=listBox içinde sol tuş ile bir eleman seçildiğinde
karşılığı textbox1'de görüntülensin (koda bakarsanız bunu yapıyor zaten)
2=sol tuş ile listBox içinde boş bir alana tıklandığında
bir seçim yapılmış ise seçim iptal olsun textbox1 de boşaltılsın
3=listBox içinde sağ tuş ile bir seçim yapılmışsa
hangi elemanın üstünde tıklandıysa o eleman seçilsin karşılığı textbox1'e yazılsın (buda kodda var zaten çalışıyor)
ListBox'a bağlanmış olan ContextMenuStrip1 açılsın (kodlar içinde var zaten çalışıyor)
4=listBox içinde sağ tuş ile boş bir bölgeye tıklanmışsa
ContextMenuStrip1 ya görünmesin yada enable özelliği False olsun
öncesinde bir seçim yapıldıysa o seçim iptal olsun textbox içi boşaltılsın

İşte arkadaşlar bu kodları if komutuyla bir türlü uygun yerlere yerleştiremedim bir sorunu düzeltiyorum başkası çıkıyor
acaba ListBox1_MouseDown ListBox1_Click olaylarındaki bu kodları uygun şekilde düzenleyebilirmisiniz

PROJE LİNGİ (VS 2010 İLE HAZIRLANDI)
http://hotfile.com/dl/125497255/bda73c0/ListBox1SecimYapma.zip.html

Kod:
Public Class Form1

    Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
        If ListBox1.SelectedIndex > -1 Then
            '-----------------------------------------------------------------------------------------------------------------------
            ' LİSTBOX İÇİNDE SEÇİLENLERİ TEXTBOX METİN İÇİNDE GÖSTERME KODU AŞAĞIDADIR
            '-----------------------------------------------------------------------------------------------------------------------
            'Bu ListBox içinde seçilen ismin 'BASLIK' , 'NOTLAR'sütünundaki karşılığını TextBox içinde görüntüler
            Me.TextBox1.DataBindings.Clear() ' Çift bağlantıyı önlemek için bağlantıyı temizler
            TextBox1.DataBindings.Add(New Binding("Text", ListBox1.SelectedItem, "NOTLAR", True, DataSourceUpdateMode.OnPropertyChanged))
        End If

    End Sub

    Private Sub ListBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox1.MouseDown

        If ListBox1.SelectedIndex > -1 Then
            If e.Button = MouseButtons.Left Then

                If ListBox1.SelectedIndex > -1 Then
                    ListBox1.Refresh()
                    Me.TextBox1.DataBindings.Clear() ' Çift bağlantıyı önlemek için bağlantıyı temizler
                    TextBox1.DataBindings.Add(New Binding("Text", ListBox1.SelectedItem, "NOTLAR", True, DataSourceUpdateMode.OnPropertyChanged))
                End If

                If ListBox1.SelectedIndex < 0 Then
                    Me.TextBox1.Clear() ' Çift bağlantıyı önlemek için bağlantıyı temizler
                    Me.TextBox1.DataBindings.Clear() ' Çift bağlantıyı önlemek için bağlantıyı temizler
                    ListBox1.ClearSelected()
                End If

            End If
            If e.Button = MouseButtons.Right Then
                If ListBox1.SelectedIndex > -1 Then
                    Dim lb As ListBox = CType(sender, ListBox)
                    Dim pt As New Point(e.X, e.Y)
                    Dim index As Integer = lb.IndexFromPoint(pt)
                    lb.SelectedIndex = index
                    Me.ContextMenuStrip1.Show(Me.ListBox1, pt)
                    ListBox1.Refresh()
                    ' Listbox'a Sağ tıklatınca sol tıklatmış gibi seçim yapar
                    'Bu ListBox içinde seçilen ismin 'BASLIK' , 'NOTLAR'sütünundaki karşılığını TextBox içinde görüntüler
                    Try
                        Me.TextBox1.DataBindings.Clear() ' Çift bağlantıyı önlemek için bağlantıyı temizler
                        TextBox1.DataBindings.Add(New Binding("Text", ListBox1.SelectedItem, "NOTLAR", True, DataSourceUpdateMode.OnPropertyChanged))
                    Catch ex As Exception

                    End Try
                Else ' sağ tuş ile bir eleman seçilmemişse
                    Me.ContextMenuStrip1.Enabled = False
                    Me.TextBox1.Clear() ' Çift bağlantıyı önlemek için bağlantıyı temizler
                    Me.TextBox1.DataBindings.Clear() ' Çift bağlantıyı önlemek için bağlantıyı temizler
                    ListBox1.ClearSelected()


                End If
                If ListBox1.SelectedIndex < 0 Then
                    Me.TextBox1.Clear() ' Çift bağlantıyı önlemek için bağlantıyı temizler
                    Me.TextBox1.DataBindings.Clear() ' Çift bağlantıyı önlemek için bağlantıyı temizler
                    ListBox1.ClearSelected()
                End If




            End If
        Else

            Me.TextBox1.Clear() ' Çift bağlantıyı önlemek için bağlantıyı temizler
            Me.TextBox1.DataBindings.Clear() ' Çift bağlantıyı önlemek için bağlantıyı temizler
            ListBox1.ClearSelected()

        End If
    End Sub

    Sub guncelle()
        Dim ds As New DataSet
        Dim da As OleDb.OleDbDataAdapter
        Dim sql, dbProvider, dbSource As String
        Dim baglanti As New OleDb.OleDbConnection
        Me.ListBox1.DataBindings.Clear()
        Me.ListBox1.Items.Clear()
        Me.ListBox1.Sorted = True

        dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
        dbSource = "Data Source=|DataDirectory|\Bilgi.mdb;Persist Security Info=True;Jet OLEDB:Database Password=#Sezgin#"
        baglanti.ConnectionString = dbProvider & dbSource
        baglanti.Open()

        sql = "SELECT * FROM Receiver ORDER BY BASLIK"
        da = New OleDb.OleDbDataAdapter(sql, baglanti)
        da.Fill(ds, "Receiver")


        Me.ListBox1.DataSource = ds.Tables(0)
        Me.ListBox1.DisplayMember = "BASLIK"
        Me.ListBox1.ValueMember = "SIRA"
        Me.ListBox1.ClearSelected()

        baglanti.Close()
        Me.Toplam.Text = ListBox1.Items.Count & "  Kayıt"
        Me.TextBox1.Clear()


    End Sub

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

    Sub sil()

        Dim baglanti As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0;Data Source=|DataDirectory|\Bilgi.mdb;Persist Security Info=True;Jet OLEDB:Database Password=#Sezgin#")

        If ListBox1.SelectedIndices.Count <= 0 Then
            Return 'Seçilen birşey yoksa çıkış
        End If

        Try
            Dim I As Integer = MsgBox("Bu kaydı silmek istiyormusunuz? Geri getirilemeyecek", MsgBoxStyle.YesNo, "Önemli Uyarı")
            If I = MsgBoxResult.Yes Then

                baglanti.Open()

                Dim cmd2 As New OleDb.OleDbCommand("DELETE FROM Receiver WHERE SIRA = " & ListBox1.SelectedValue, baglanti) 'ListBox1.Items(ItemNo).SubItems(0).Text, conn)
                cmd2.ExecuteNonQuery()
                MsgBox("Kayıt başarıyla silindi", MsgBoxStyle.OkOnly, "Silme Başarılı")

            Else
                'They didn't really want to delete, so exit
                Return 'This exits the sub
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            baglanti.Close()
        End Try

        '-----------------------------------------------------------------------------------------------
        'Bu kod Anasayfadaki listbox'u günceller
        Dim ds As New DataSet
        Dim da As OleDb.OleDbDataAdapter
        Dim sql As String
        sql = "SELECT * FROM Receiver ORDER BY BASLIK"
        da = New OleDb.OleDbDataAdapter(sql, baglanti)
        ds.Tables.Clear()
        da.Fill(ds, "Receiver")
        Me.ListBox1.DataSource = ds.Tables(0)
        Me.ListBox1.DisplayMember = "BASLIK"
        Me.ListBox1.SelectedItem = "BASLIK"
        baglanti.Close()

    End Sub

    Private Sub Test2ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Test2ToolStripMenuItem.Click
        sil()
    End Sub

    Private Sub Test1ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Test1ToolStripMenuItem.Click
        Form2.Show()
        Me.Hide()
    End Sub
End Class



' ***************************************************************************************
' FORM2'NİN KODLARI
' ***************************************************************************************
Imports System.Data.OleDb

Public Class Form2
    Private baglanti As New OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" & Application.StartupPath & "/Bilgi.mdb;Persist Security Info=True;Jet OLEDB:Database Password=#Sezgin#")
    Private komut As New OleDbCommand()
    Private guc As New OleDbDataAdapter()
    Private gecici_tablo As New DataSet()
    Dim bugün As String = Format(Date.Now, "dd/MM/yyyy")

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If String.IsNullOrEmpty(TextBox1.Text) Then
            MessageBox.Show("Mutlaka Bir İsim Girilmelidir", "UYARI")
            TextBox1.Focus()
            Return
        End If

        baglanti.Open()
        komut.Connection = baglanti
        komut.CommandText = (("INSERT INTO Receiver(BASLIK,NOTLAR,TARIH) VALUES ('" + TextBox1.Text & "','") + TextBox2.Text & "','") + bugün & "') "
        komut.ExecuteNonQuery()
        komut.Dispose()
        baglanti.Close()
        Me.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub

    Private Sub listele()
        Dim ds As New DataSet
        Dim dbProvider, dbSource As String
        Dim con As New OleDb.OleDbConnection

        dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
        dbSource = "Data Source=|DataDirectory|\Bilgi.mdb;Persist Security Info=True;Jet OLEDB:Database Password=#Sezgin#"

        con.ConnectionString = dbProvider & dbSource

        Form1.ListBox1.DataSource = Nothing
        Form1.ListBox1.DisplayMember = Nothing
        Form1.ListBox1.ValueMember = Nothing
        Form1.ListBox1.SelectedItem = Nothing
        con.Open()

        Dim guc As New OleDbDataAdapter("select * From Receiver", con)
        guc.Fill(ds, "Receiver")

        Form1.ListBox1.DataSource = ds.Tables(0)
        Form1.ListBox1.DisplayMember = "BASLIK"
        Form1.ListBox1.ValueMember = "SIRA"
        Form1.ListBox1.SelectedItem = "BASLIK"

        guc.Dispose()
        con.Close()

    End Sub

    Private Sub Form2_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        listele()
        Form1.Show()
    End Sub
End Class
 
Üst