Загрузка данных


using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp6
{
    public partial class Form1 : Form
    {
        private const string FileName = "tasks.txt";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string taskText = txtTaskInput.Text.Trim();

            if (!string.IsNullOrEmpty(taskText))
            {
                lstTasks.Items.Add(taskText);
                txtTaskInput.Clear(); 
                txtTaskInput.Focus(); 
            }
            else
            {
                MessageBox.Show("Введите текст задачи!", "Предупреждение", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (lstTasks.SelectedIndex != -1)
            {
                lstTasks.Items.RemoveAt(lstTasks.SelectedIndex);
            }
            else
            {
                MessageBox.Show("Выберите задачу для удаления!", "Предупреждение", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                
                string[] tasks = lstTasks.Items.Cast<string>().ToArray();
                File.WriteAllLines(FileName, tasks);
                MessageBox.Show("Задачи успешно сохранены в файл!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Ошибка при сохранении файла: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            if (File.Exists(FileName))
            {
                try
                {
                    lstTasks.Items.Clear(); 
                    string[] tasks = File.ReadAllLines(FileName);
                    lstTasks.Items.AddRange(tasks);
                    MessageBox.Show("Задачи успешно загружены!", "Успех", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Ошибка при чтении файла: {ex.Message}", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show($"Файл '{FileName}' не найден!", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        

        private void txtTaskInput_TextChanged(object sender, EventArgs e)
        {

        }
    }
}