コード例 #1
0
 private void ButtonSave_Click(object sender, EventArgs e)
 {
     if (ActionInput.Text == "Добавить")
     {
         try
         {
             if (!ValidatorInput() && !string.IsNullOrEmpty(Gender.Text))
             {
                 var sportsman = GetDataFromInputs();
                 listBox.Items.Add($"{sportsman.FullName} ({sportsman.Team})\t{sportsman.DateTime}");
                 Sportsmen.Add(sportsman);
                 _iOData.SaveData(Sportsmen);
                 MessageBox.Show($"Спортсмен: \"{sportsman.FullName}\"\n успешно сохранен!", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
                 ClearInputs();
             }
             else
             {
                 throw new Exception("Некорректные данные");
             }
         }
         catch
         {
             MessageBox.Show($"Некорректные данные!", "Предупреждение", MessageBoxButtons.OK, MessageBoxIcon.Warning);
         }
     }
     else if (ActionInput.Text == "Удалить")
     {
         int index = listBox.SelectedIndex;
         MessageBox.Show($"Спортсмен {Sportsmen[index].FullName} успешно удален!", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
         listBox.Items.RemoveAt(index);
         Sportsmen.RemoveAt(index);
         _iOData.SaveData(Sportsmen);
         ClearInputs();
     }
     else if (ActionInput.Text == "Редактировать")
     {
         if (listBox.SelectedIndex > -1)
         {
             try
             {
                 if (!ValidatorInput() && !string.IsNullOrEmpty(Gender.Text))
                 {
                     var sportsman = GetDataFromInputs();
                     Sportsmen[listBox.SelectedIndex]     = sportsman;
                     listBox.Items[listBox.SelectedIndex] = $"{sportsman.FullName} ({sportsman.Team})\t{sportsman.DateTime}";
                     MessageBox.Show($"Данные спортсмена: \"{sportsman.FullName}\"\n успешно изменены!", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
                     _iOData.SaveData(Sportsmen);
                 }
                 else
                 {
                     throw new Exception("Некорректные данные");
                 }
             }
             catch
             {
                 MessageBox.Show($"Некорректные данные!", "Предупреждение", MessageBoxButtons.OK, MessageBoxIcon.Warning);
             }
         }
     }
 }