using System; using System.Windows.Forms; namespace WindowsFormsApp4 { public partial class AddressEditForm : Form { private int? addressId; private bool isEditMode; public AddressEditForm() { InitializeComponent(); isEditMode = false; lblTitle.Text = "Добавление адреса"; AttachEventHandlers(); } public AddressEditForm(int id, string city, string street, string house, string building, string apartment) { InitializeComponent(); isEditMode = true; addressId = id; lblTitle.Text = "Редактирование адреса"; txtCity.Text = city; txtStreet.Text = street; txtHouse.Text = house; txtBuilding.Text = building; txtApartment.Text = apartment; AttachEventHandlers(); } private void AttachEventHandlers() { btnSave.Click += BtnSave_Click; btnCancel.Click += BtnCancel_Click; } private bool ValidateFields() { if (string.IsNullOrWhiteSpace(txtCity.Text)) { MessageBox.Show("Введите город", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtCity.Focus(); return false; } if (string.IsNullOrWhiteSpace(txtStreet.Text)) { MessageBox.Show("Введите улицу", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtStreet.Focus(); return false; } if (string.IsNullOrWhiteSpace(txtHouse.Text)) { MessageBox.Show("Введите номер дома", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning); txtHouse.Focus(); return false; } if (cmbAddressType.SelectedItem == null) { MessageBox.Show("Выберите тип адреса", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Warning); cmbAddressType.Focus(); return false; } return true; } private void BtnSave_Click(object sender, EventArgs e) { if (!ValidateFields()) return; bool result; if (isEditMode && addressId.HasValue) { result = DatabaseHelper.UpdateAddress( addressId.Value, txtCity.Text.Trim(), txtStreet.Text.Trim(), txtHouse.Text.Trim(), txtBuilding.Text.Trim(), txtApartment.Text.Trim() ); } else { int newAddressId = DatabaseHelper.AddAddress( txtCity.Text.Trim(), txtStreet.Text.Trim(), txtHouse.Text.Trim(), txtBuilding.Text.Trim(), txtApartment.Text.Trim() ); result = newAddressId > 0; if (result && Session.CurrentUser.ClientId.HasValue) { result = DatabaseHelper.AddClientAddress( Session.CurrentUser.ClientId.Value, newAddressId, cmbAddressType.SelectedItem.ToString(), 10 ); } } if (result) { this.DialogResult = DialogResult.OK; this.Close(); } else { MessageBox.Show("Ошибка при сохранении", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void BtnCancel_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); } } }