示例#1
0
        private void AddAnimalBTN_Click(object sender, EventArgs e)
        {
            //Check wich radiobutton is selected in both the group boxes and make a new animal size int
            var sizeButton = AnimalSizeGB.Controls.OfType <RadioButton>()
                             .FirstOrDefault(r => r.Checked);
            var dietButton = AnimalDietGB.Controls.OfType <RadioButton>()
                             .FirstOrDefault(r => r.Checked);


            //Cast the given strings from the radiobuttons in to a diet and size enum
            Diet       dietName   = (Diet)Diet.Parse(typeof(Diet), dietButton.Text);
            AnimalSize animalSize = (AnimalSize)Enum.Parse(typeof(AnimalSize), sizeButton.Text);

            //Make a new animal and add it to the List View/List Box
            Animal animal = new Animal(animalSize, dietName);

            //AnimalLV.Items.Add($"Animal size: {animal.AnimalSize} and diet: {animal.Diet}");
            AnimalLB.Items.Add($"Animal size: {animal.AnimalSize} and diet: {animal.Diet}");
            Animals.Add(animal);
        }