using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Projekt_po
{
    public partial class Lekarze : Form
    {
       
        private List<Lekarz> lekarze = new List<Lekarz>();

        public Lekarze()
        {
            InitializeComponent();
        }

        private void DodajLekarza(Lekarz l)
        {
            lekarze.Add(l);
        }

        private void DodajLekarzaDoDataGridView(Lekarz l)
        {
            dataGridView1.Rows.Add(l.ImieNazwisko, l.Specjalnosc, l.GodzPrzyjec, l.Inne);
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "" ||
                textBox4.Text == "" || textBox5.Text == "")
                MessageBox.Show("Uzupelnij wszystkie pola");
            else
            {
                try
                {
                    Lekarz l = new Lekarz(textBox1.Text, textBox2.Text, textBox3.Text,
                    Convert.ToInt32(textBox4.Text), textBox5.Text);
                    DodajLekarza(l);
                    DodajLekarzaDoDataGridView(l);
                    textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; textBox5.Text = "";
                    this.DialogResult = DialogResult.OK;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Blad: " + ex.Message);
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; textBox5.Text = "";
            dataGridView1.Rows.Clear();
            lekarze.Clear();
            MessageBox.Show("Wyczyszczono wszystko");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = "";
            saveFileDialog1.Filter = "Plik tekstowy (*.txt)|*.txt|Wszystkie pliki (*.*)|*.*";
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.FileName = "lekarze";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                for (int i = 0; i < lekarze.Count; i++)
                {
                    sw.WriteLine("[LEKARZ]");
                    sw.WriteLine("[" + dataGridView1.Columns[0].Name + "]");
                    sw.WriteLine(lekarze[i].ImieNazwisko);
                    sw.WriteLine("[" + dataGridView1.Columns[1].Name + "]");
                    sw.WriteLine(lekarze[i].Specjalnosc);
                    sw.WriteLine("[" + dataGridView1.Columns[2].Name + "]");
                    sw.WriteLine(lekarze[i].GodzPrzyjec);
                    sw.WriteLine("[" + dataGridView1.Columns[3].Name + "]");
                    sw.WriteLine(lekarze[i].LiczbaPacjentow);
                    sw.WriteLine("[" + dataGridView1.Columns[4].Name + "]");
                    sw.WriteLine(lekarze[i].Inne);
                    sw.WriteLine("[END LEKARZ]");
                    sw.WriteLine();
                }
                sw.Close();
                fs.Close();
                MessageBox.Show("Zapisano do pliku \"lekarze.txt\"");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //if (File.Exists("./lekarze.txt"))
            //{
                    OpenFileDialog openFileDialog1 = new OpenFileDialog();
                    openFileDialog1.InitialDirectory = "";
                    openFileDialog1.Filter = "Plik tekstowy (*.txt)|*.txt|Wszystkie pliki (*.*)|*.*";
                    openFileDialog1.RestoreDirectory = true;
                    openFileDialog1.FileName = "lekarze";
                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open);
                        StreamReader sr = new StreamReader(fs);
                        dataGridView1.Rows.Clear();
                        lekarze.Clear();
                        string linia;
                        Lekarz l;
                        try
                        {
                            while ((linia = sr.ReadLine()) != null)
                            {
                                if (linia == "[LEKARZ]")
                                {
                                    l = new Lekarz();
                                    while ((linia = sr.ReadLine()) != "[END LEKARZ]")
                                    {
                                        if (linia == ("[" + dataGridView1.Columns[0].Name + "]"))
                                        {
                                            l.ImieNazwisko = sr.ReadLine();
                                            continue;
                                        }
                                        if (linia == ("[" + dataGridView1.Columns[1].Name + "]"))
                                        {
                                            l.Specjalnosc = sr.ReadLine();
                                            continue;
                                        }
                                        if (linia == ("[" + dataGridView1.Columns[2].Name + "]"))
                                        {
                                            l.GodzPrzyjec = sr.ReadLine();
                                            continue;
                                        }
                                        if (linia == ("[" + dataGridView1.Columns[3].Name + "]"))
                                        {
                                            l.LiczbaPacjentow = Convert.ToInt32(sr.ReadLine());
                                            continue;
                                        }
                                        if (linia == ("[" + dataGridView1.Columns[4].Name + "]"))
                                        {
                                            l.Inne = sr.ReadLine();
                                        }
                                    }
                                    DodajLekarza(l);
                                    DodajLekarzaDoDataGridView(l);
                                }
                            }
                            this.DialogResult = DialogResult.OK;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Blad: " + ex.Message);
                        }
                        finally
                        {
                            sr.Close();
                            fs.Close();
                            if (this.DialogResult == DialogResult.OK)
                                MessageBox.Show("Odczytano z pliku \"lekarze.txt\"");
                        }
                    }
            //}
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = "";
            saveFileDialog1.Filter = "Plik xml (*.xml)|*.xml|Wszystkie pliki (*.*)|*.*";
            saveFileDialog1.RestoreDirectory = true;
            saveFileDialog1.FileName = "lekarze";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                
                FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
                XmlSerializer serializer = new XmlSerializer(typeof(List<Lekarz>));
                serializer.Serialize(fs, lekarze);
                fs.Close();
                MessageBox.Show("Zserializowano do pliku \"lekarze.xml\"");
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            //if (File.Exists("./lekarze.xml"))
            //{
                    OpenFileDialog openFileDialog1 = new OpenFileDialog();
                    openFileDialog1.InitialDirectory = "";
                    openFileDialog1.Filter = "Plik xml (*.xml)|*.xml|Wszystkie pliki (*.*)|*.*";
                    openFileDialog1.RestoreDirectory = true;
                    openFileDialog1.FileName = "lekarze";
                    if (openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.Open);
                        XmlSerializer serializer = new XmlSerializer(typeof(List<Lekarz>));
                        dataGridView1.Rows.Clear();
                        try
                        {
                            lekarze = (List<Lekarz>)serializer.Deserialize(fs);
                            for (int i = 0; i < lekarze.Count; i++)
                            {
                                DodajLekarzaDoDataGridView(lekarze[i]);
                            }
                            this.DialogResult = DialogResult.OK;
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Blad: " + ex.Message);
                        }
                        finally
                        {
                            fs.Close();
                            if (this.DialogResult == DialogResult.OK)
                                MessageBox.Show("Deserializowano z pliku \"lekarze.xml\"");
                        }
                    }
            //}
        }

    }
}

© Wklejaj.pl