示例#1
0
        private void button1_Click(object sender, EventArgs e)
        {
            if ((textBoxPESEL.Text == "") || (textBoxNazwisko.Text == "") || (textBoxImie.Text == ""))
            {
                MessageBox.Show("Nie prowadzono danej w polu PESEL, Imie lub Nazwisko!", "Błąd", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Reset();
            }

            int           grade         = int.Parse(checkedListBoxOcena.SelectedItem.ToString());
            SchoolSubject schoolSubject = (SchoolSubject)Enum.Parse(typeof(SchoolSubject), checkedListBoxPrzedmiot.SelectedItem.ToString(), false);
            SchoolClass   schoolClass   = (SchoolClass)Enum.Parse(typeof(SchoolClass), checkedListBoxClass.SelectedItem.ToString(), false);
            string        name          = textBoxImie.Text;
            string        secondName    = textBoxNazwisko.Text;
            string        id            = textBoxPESEL.Text;
            int           idInt;

            //MessageBox.Show(grade + ""+ schoolClass +""+ schoolSubject + name + secondName + id);

            //sprawdzenie czy pesel jest liczba, jesli nie = komunikat
            if (!int.TryParse(id, out idInt))
            {
                MessageBox.Show("Pesel nie zawiera samych liczb, czyżby nastąpił błąd?", "Uwaga", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Warning);
            }

            //tworzenie obiektu
            PreparedLineFormatFile preparedLineFormatFile = new PreparedLineFormatFile(idInt, name + secondName, schoolClass, schoolSubject, grade);

            //wysylanie do pliku
            LineObject.SendLineToFile(preparedLineFormatFile);


            Reset();
        }
        public static void SendLineToFile(PreparedLineFormatFile preparedLineFormatFile)
        {
            string       path = "DataFile.txt";
            StreamWriter sw   = new StreamWriter(path, true);

            sw.Write(preparedLineFormatFile.ID);
            sw.Write(",");
            sw.Write(preparedLineFormatFile.Name);
            sw.Write(",");
            sw.Write(preparedLineFormatFile.SchoolClass);
            sw.Write(",");
            sw.Write(preparedLineFormatFile.SchoolSubject);
            sw.Write(",");
            sw.Write(preparedLineFormatFile.Grade);
            sw.WriteLine();

            sw.Close();
        }
        //usuwa jeden, nie wszystkie te same
        public static void DeleteLineFromFile(PreparedLineFormatFile pLFF)
        {
            var          list = GetDataFromFile.GetData();
            const string path = "DataFile.txt";

            //czyszcze plik bo się nadpisze podwojnie
            StreamWriter sw1 = new StreamWriter(path, false);

            sw1.Close();

            var listNew = new List <PreparedLineFormatFile>();

            StreamReader sr      = new StreamReader(path);
            int          counter = 0; //licznik by tylko 1 linia została pobrana

            foreach (var x in GetDataFromFile.GetData())
            {
                //jeśli linie są różne, wiec nie usuwamy
                if (!((x.ID == pLFF.ID) && (x.SchoolSubject == pLFF.SchoolSubject) && (x.Grade == pLFF.Grade)))
                {
                    listNew.Add(x);
                }
                ///jesli są te same
                else if (counter == 0)
                {
                    counter++;
                }
                else
                {
                    listNew.Add(x);
                }
            }
            sr.Close();

            foreach (var x in listNew)
            {
                SendLineToFile(x);
            }
        }