private void animalComboBox_Changed(object sender, EventArgs e) { AnimalChoiceItem selectedItem = animalChoices.First(item => item.AnimalNameComboBox == (ComboBox)sender); int animalId = Int32.Parse(selectedItem.AnimalNameComboBox.SelectedValue.ToString()); foreach (Control item in AnimalGroupBox.Controls) { if (item.GetType() == typeof(ComboBox)) { if ((ComboBox)item != (ComboBox)sender && ((ComboBox)(item)).SelectedValue != null) { if (animalId.ToString() == ((ComboBox)(item)).SelectedValue.ToString()) { MessageBox.Show("Zvíře už je vybráno"); selectedItem.AnimalRace.Text = " "; selectedItem.AnimalId.Text = " "; selectedItem.AnimalGroupId.Text = " "; ((ComboBox)sender).SelectedIndexChanged -= animalComboBox_Changed; ((ComboBox)sender).SelectedIndex = -1; ((ComboBox)sender).SelectedIndexChanged += animalComboBox_Changed; return; } } } } Animal animal = animals.First(item => item.Id == animalId); selectedItem.AnimalRace.Text = animal.Race; selectedItem.AnimalId.Text = animal.Id.ToString(); selectedItem.AnimalGroupId.Text = animal.AnimalGroup.Id.ToString(); }
private void minusButton_Click(object sender, EventArgs e) { int removedIndex = animalChoices.IndexOf(animalChoices.First(item => item.MinusButton == (Button)sender)); AnimalChoiceItem deletedItem = animalChoices.First(item => item.MinusButton == (Button)sender); int removedIndexInGroupBox = AnimalGroupBox.Controls.IndexOf(deletedItem.MinusButton); for (int i = 0; i < 5; i++) { AnimalGroupBox.Controls.RemoveAt(removedIndexInGroupBox); removedIndexInGroupBox--; } animalChoices.Remove(deletedItem); this.nextAnimalChoiceItemPossition.Y -= (INPUT_HEIGHT + 10); if (animalChoices.Count != removedIndex) { for (int i = removedIndex; i < animalChoices.Count; i++) { AnimalChoiceItem item = animalChoices[i]; item.AnimalGroupId.Top -= (INPUT_HEIGHT + 10); item.AnimalId.Top -= (INPUT_HEIGHT + 10); item.AnimalNameComboBox.Top -= (INPUT_HEIGHT + 10); item.AnimalRace.Top -= (INPUT_HEIGHT + 10); item.MinusButton.Top -= (INPUT_HEIGHT + 10); } } AnimalGroupBox.Height -= INPUT_HEIGHT + 10; AddAnimalButton.Top -= (INPUT_HEIGHT + 10); Description.Top -= INPUT_HEIGHT + 10; DescrLabel.Top -= INPUT_HEIGHT + 10; AddAnimalEventButton.Top -= INPUT_HEIGHT + 10; this.Height -= INPUT_HEIGHT + 10; }