private void editAuto_Click(object sender, EventArgs e) { if (shopList.SelectedItem == null || autosList.SelectedItem == null) { return; } AddEditAuto form = new AddEditAuto(); Auto auto = shops[shopList.SelectedIndex].autos[autosList.SelectedIndex]; form.manufacturer.Text = auto.manufacturer; form.brend.Text = auto.brend; form.releaseDate.Value = auto.realeseDate; form.power.Value = auto.power; form.transmission.SelectedIndex = (int)auto.transmission.transmission - 1; form.state.Text = auto.state.state == State.StateEnum.New ? "Новый" : auto.state.distance.ToString(); form.price.Value = auto.price; form.Text = "Изменить автомобиль"; try { if (form.ShowDialog() == DialogResult.OK) { string manufacturer = form.manufacturer.Text; string brend = form.brend.Text; DateTime releaseDate = form.releaseDate.Value; decimal power = form.power.Value; Transmission transmission = Transmission.GetFromString((string)form.transmission.SelectedItem); decimal distance; State state; if (decimal.TryParse(form.state.Text, out distance)) { state = State.GetFromValue(distance); } else { state = State.GetFromString(form.state.Text); } decimal price = form.price.Value; if (manufacturer == "" || brend == "") { throw new ArgumentException("Были введены не все данные"); } auto.manufacturer = manufacturer; auto.brend = brend; auto.realeseDate = releaseDate; auto.power = power; auto.transmission = transmission; auto.state = state; auto.price = price; autosList.Items.Clear(); autosList.Items.AddRange((shopList.SelectedItem as Shop).autos.ToArray()); } } catch (ArgumentException ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
private void addAuto_Click(object sender, EventArgs e) { if (shopList.SelectedItem == null) { return; } AddEditAuto form = new AddEditAuto(); form.Text = "Добавить автомобиль"; try { if (form.ShowDialog() == DialogResult.OK) { string manufacturer = form.manufacturer.Text; string brend = form.brend.Text; DateTime releaseDate = form.releaseDate.Value; decimal power = form.power.Value; Transmission transmission = Transmission.GetFromString((string)form.transmission.SelectedItem); decimal distance; State state; if (decimal.TryParse(form.state.Text, out distance)) { state = State.GetFromValue(distance); } else { state = State.GetFromString(form.state.Text); } decimal price = form.price.Value; if (manufacturer == "" || brend == "") { throw new ArgumentException("Были введены не все данные"); } Auto auto = new Auto(manufacturer, brend, releaseDate, power, transmission, state, price, shopList.SelectedItem as Shop); (shopList.SelectedItem as Shop).autos.Add(auto); autosList.Items.Clear(); autosList.Items.AddRange((shopList.SelectedItem as Shop).autos.ToArray()); } } catch (ArgumentException ex) { MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } }