C# Da Dosyadan Veri Okuma ve yazma Listbox ile

Arkadaşlar bu yazımda , listbox  ' a  veri ekleme , silme ,ve DOSYADAN VERİ OKUMA VE YAZMA işlemlerimi anlatacağım , umarım faydalı olur , herhangi bir problem yaşayan arkadaşlar yorum bölümüne problemlerini yazarsa yardımcı olabilirim .


Yapacağımız Uygulama 



 private void ListeyeEkle_Click(object sender, EventArgs e)
        {
            /**İlk Başta Textboxlarımızda veri varmı Yokmu Diye Kontrol Edelim */
            if (txt_ogrNo.Text == "" || txt_adSoyad.Text == "" || cbx_Bolum.SelectedIndex == -1)
            {
                MessageBox.Show("Boş Bırakılan Yerler var ! ");
                return;

            }
            /*Daha Sonra ListBox ' a veri yüklemek için bir değişken tanımlıyoruz */
            else
            {
                string veri = txt_ogrNo.Text + "-" + txt_adSoyad.Text + "-" + cbx_Bolum.SelectedItem.ToString();
                if (listBox1.Items.Contains(veri))
                { 
                
                  MessageBox.Show("Aynı Verilerden Daha Önce Eklenmiş ! "); 
                }
                listBox1.Items.Add(veri);
            
            }
        }

 private void ListedenSil_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                listBox1.Items.Remove(listBox1.SelectedItem);
            
            }
        }
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1)
            {
                return;

                string[] parcala = listBox1.SelectedItem.ToString().Split('-');

                txt_ogrNo.Text=parcala[0];
                txt_adSoyad.Text = parcala[1];
                cbx_Bolum.SelectedItem=parcala[2];
            
            }

private void DosyaKaydet_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Filter = "ogrencikayit|*.abc|Text|*.txt|Tüm Dosyalar|*.*";
            saveFileDialog1.FilterIndex = 1;
            DialogResult dk = saveFileDialog1.ShowDialog();

            if (dk == DialogResult.Yes)
            {
                try
                {

                    StreamWriter s = new StreamWriter(saveFileDialog1.FileName);
                    for (int i = 0; i < listBox1.Items.Count; i++)
                    {

                        s.WriteLine(listBox1.Items[i].ToString());

                    }
                    s.Close();
                    MessageBox.Show("Veriler Kaydedildi");
                }
                catch {

                    MessageBox.Show("HATA OLUŞTU... ! ");
                
                }
            }
    }



 private void DosyadanOku_Click(object sender, EventArgs e)

        {
             {
            openFileDialog1.Filter = "ogrencikayit|*.abc|Text|*.txt|Tüm Dosyalar|*.*";
            openFileDialog1.FilterIndex = 1;
            DialogResult A = openFileDialog1.ShowDialog();
            if (A==DialogResult.OK)
            { 
            
            StreamReader sr= new StreamReader(openFileDialog1.FileName);
                string alinan = sr.ReadLine();
                    while(alinan !=null)
                    {
                    listBox1.Items.Add(alinan);
                        alinan = sr.ReadLine();

                    
                    }
                sr.Close();
            
            }
        }
        }
     
    }


2 yorum:

 
© 2014 İrfan Blog ' s | Mehmet İrfan ErdoğanTüm Hakları Saklıdır | r