Esempio n. 1
0
        AdressBookMember[] Members = new AdressBookMember[0];//объявления массива объектов класса AdressBookMember
        // Инициализация формы
        public Form1()
        {
            InitializeComponent();
            int count = 0;

            count = CountLines(count);
            if (count != 0)
            {
                for (int j = 0; j < count; j++)
                {
                    try
                    {
                        Members[j] = new AdressBookMember();
                    }
                    catch (Exception)
                    {
                        Array.Resize(ref Members, Members.Length + 1);
                        Members[j] = new AdressBookMember();
                    }
                }
                int i     = 0;
                var lines = File.ReadLines("data.txt");
                foreach (var line in lines)
                {
                    Members[i].GetDataFromFile(line);
                    dataGridView1.Rows.Add(Members[i].number.ToString(), Members[i].secondname, Members[i].firstname, Members[i].age, Members[i].telnumber);
                    i++;
                }
            }
        }
Esempio n. 2
0
        //Обработка события нажатия на кнопку "Save"
        private void SaveBut_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount == 1)
            {
                File.Delete("data.txt");
                File.Create("data.txt").Close();
                MessageBox.Show("File saved");
                return;
            }
            string str = String.Empty;

            for (int i = 0; i < dataGridView1.RowCount - 1; i++)
            {
                for (int j = 0; j != 5; j++)
                {
                    try
                    {
                        str += dataGridView1.Rows[i].Cells[j].Value.ToString() + ";";
                    }
                    catch (Exception)
                    {
                        dataGridView1.Rows[i].Cells[j].Value = "0";
                        str += dataGridView1.Rows[i].Cells[j].Value + ";";
                    }
                }
                try
                {
                    Members[i].GetDataFromFile(str);
                }
                catch (Exception)
                {
                    Array.Resize(ref Members, Members.Length + 1);
                    Members[i] = new AdressBookMember();
                    Members[i].GetDataFromFile(str);
                }
                Array.Resize(ref Members, dataGridView1.RowCount - 1);
                str = String.Empty;
            }
            Members[0].SaveDataInFile(Members);
        }