ClientEditForm.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Text.RegularExpressions;
  3. using System.Windows.Forms;
  4. namespace WindowsFormsApp4
  5. {
  6. public partial class ClientEditForm : Form
  7. {
  8. private int? clientId;
  9. private bool isEditMode;
  10. public ClientEditForm()
  11. {
  12. InitializeComponent();
  13. isEditMode = false;
  14. lblTitle.Text = "Добавление клиента";
  15. AttachEventHandlers();
  16. }
  17. public ClientEditForm(int id, string lastName, string firstName, string middleName, string phone)
  18. {
  19. InitializeComponent();
  20. isEditMode = true;
  21. clientId = id;
  22. lblTitle.Text = "Редактирование клиента";
  23. txtLastName.Text = lastName;
  24. txtFirstName.Text = firstName;
  25. txtMiddleName.Text = middleName;
  26. txtPhone.Text = phone;
  27. AttachEventHandlers();
  28. }
  29. private void AttachEventHandlers()
  30. {
  31. btnSave.Click += BtnSave_Click;
  32. btnCancel.Click += BtnCancel_Click;
  33. }
  34. private bool ValidateFields()
  35. {
  36. if (string.IsNullOrWhiteSpace(txtLastName.Text))
  37. {
  38. MessageBox.Show("Введите фамилию", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  39. txtLastName.Focus();
  40. return false;
  41. }
  42. if (string.IsNullOrWhiteSpace(txtFirstName.Text))
  43. {
  44. MessageBox.Show("Введите имя", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  45. txtFirstName.Focus();
  46. return false;
  47. }
  48. string phone = txtPhone.Text.Trim();
  49. if (string.IsNullOrWhiteSpace(phone))
  50. {
  51. MessageBox.Show("Введите номер телефона", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  52. txtPhone.Focus();
  53. return false;
  54. }
  55. Regex phoneRegex = new Regex(@"^(\+7|8)[0-9]{10}$");
  56. if (!phoneRegex.IsMatch(phone))
  57. {
  58. MessageBox.Show("Некорректный номер телефона. Формат: +7XXXXXXXXXX или 8XXXXXXXXXX",
  59. "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  60. txtPhone.Focus();
  61. return false;
  62. }
  63. return true;
  64. }
  65. private void BtnSave_Click(object sender, EventArgs e)
  66. {
  67. if (!ValidateFields())
  68. return;
  69. bool result;
  70. if (isEditMode && clientId.HasValue)
  71. {
  72. result = DatabaseHelper.UpdateClient(
  73. clientId.Value,
  74. txtLastName.Text.Trim(),
  75. txtFirstName.Text.Trim(),
  76. txtMiddleName.Text.Trim(),
  77. txtPhone.Text.Trim()
  78. );
  79. }
  80. else
  81. {
  82. int newId = DatabaseHelper.AddClient(
  83. txtLastName.Text.Trim(),
  84. txtFirstName.Text.Trim(),
  85. txtMiddleName.Text.Trim(),
  86. txtPhone.Text.Trim()
  87. );
  88. result = newId > 0;
  89. }
  90. if (result)
  91. {
  92. this.DialogResult = DialogResult.OK;
  93. this.Close();
  94. }
  95. else
  96. {
  97. MessageBox.Show("Ошибка при сохранении", "Ошибка",
  98. MessageBoxButtons.OK, MessageBoxIcon.Error);
  99. }
  100. }
  101. private void BtnCancel_Click(object sender, EventArgs e)
  102. {
  103. this.DialogResult = DialogResult.Cancel;
  104. this.Close();
  105. }
  106. }
  107. }