AddressEditForm.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Windows.Forms;
  3. namespace WindowsFormsApp4
  4. {
  5. public partial class AddressEditForm : Form
  6. {
  7. private int? addressId;
  8. private bool isEditMode;
  9. public AddressEditForm()
  10. {
  11. InitializeComponent();
  12. isEditMode = false;
  13. lblTitle.Text = "Добавление адреса";
  14. AttachEventHandlers();
  15. }
  16. public AddressEditForm(int id, string city, string street, string house, string building, string apartment)
  17. {
  18. InitializeComponent();
  19. isEditMode = true;
  20. addressId = id;
  21. lblTitle.Text = "Редактирование адреса";
  22. txtCity.Text = city;
  23. txtStreet.Text = street;
  24. txtHouse.Text = house;
  25. txtBuilding.Text = building;
  26. txtApartment.Text = apartment;
  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(txtCity.Text))
  37. {
  38. MessageBox.Show("Введите город", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  39. txtCity.Focus();
  40. return false;
  41. }
  42. if (string.IsNullOrWhiteSpace(txtStreet.Text))
  43. {
  44. MessageBox.Show("Введите улицу", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  45. txtStreet.Focus();
  46. return false;
  47. }
  48. if (string.IsNullOrWhiteSpace(txtHouse.Text))
  49. {
  50. MessageBox.Show("Введите номер дома", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  51. txtHouse.Focus();
  52. return false;
  53. }
  54. if (cmbAddressType.SelectedItem == null)
  55. {
  56. MessageBox.Show("Выберите тип адреса", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  57. cmbAddressType.Focus();
  58. return false;
  59. }
  60. return true;
  61. }
  62. private void BtnSave_Click(object sender, EventArgs e)
  63. {
  64. if (!ValidateFields())
  65. return;
  66. bool result;
  67. if (isEditMode && addressId.HasValue)
  68. {
  69. result = DatabaseHelper.UpdateAddress(
  70. addressId.Value,
  71. txtCity.Text.Trim(),
  72. txtStreet.Text.Trim(),
  73. txtHouse.Text.Trim(),
  74. txtBuilding.Text.Trim(),
  75. txtApartment.Text.Trim()
  76. );
  77. }
  78. else
  79. {
  80. int newAddressId = DatabaseHelper.AddAddress(
  81. txtCity.Text.Trim(),
  82. txtStreet.Text.Trim(),
  83. txtHouse.Text.Trim(),
  84. txtBuilding.Text.Trim(),
  85. txtApartment.Text.Trim()
  86. );
  87. result = newAddressId > 0;
  88. if (result && Session.CurrentUser.ClientId.HasValue)
  89. {
  90. result = DatabaseHelper.AddClientAddress(
  91. Session.CurrentUser.ClientId.Value,
  92. newAddressId,
  93. cmbAddressType.SelectedItem.ToString(),
  94. 10
  95. );
  96. }
  97. }
  98. if (result)
  99. {
  100. this.DialogResult = DialogResult.OK;
  101. this.Close();
  102. }
  103. else
  104. {
  105. MessageBox.Show("Ошибка при сохранении", "Ошибка",
  106. MessageBoxButtons.OK, MessageBoxIcon.Error);
  107. }
  108. }
  109. private void BtnCancel_Click(object sender, EventArgs e)
  110. {
  111. this.DialogResult = DialogResult.Cancel;
  112. this.Close();
  113. }
  114. }
  115. }