Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private Animal AddAnimal()
        {
            string name = txtName.Text;
            double age = Convert.ToDouble(txtAge.Text);

            CategoryType animalCategory = (CategoryType)lstCategory.SelectedIndex;
            Animal animalItem = null;

            switch (animalCategory)
            {
                case CategoryType.Bird:
                    Bird.BirdSpecies birdSpecie = (Bird.BirdSpecies)Enum.Parse(typeof(Bird.BirdSpecies), lstCategory.SelectedItem.ToString());
                    animalItem = Bird.BirdFactory.CreateBird(birdSpecie);
                    break;
                    
                case.... etc.


My problem mainly revolves around trying to bind the "new" datasource for my second list depending on the choice in the first list. For instance if the bird category is chosen, how would I go about to bind the specific enums (e.g. bee, butterfly) to the second list? Do I need to use the eventhandler SelectedIndexChanged, if so, how would that be done most plausible?

Having the method run under SelectedIndexChanged seems to work. However, the binding part is more troublesome, I can't figure it out. It seems abundant to have "lstCategory.SelectedItem" go off as the string value in Enum.Parse, or am I out in the dark?
Posted
Updated 3-Feb-13 10:35am
v5
Comments
Sergey Alexandrovich Kryukov 3-Feb-13 20:27pm    
The whole idea looks so wrong! You replace the classification with enumeration and switch. Looks like a total abuse. I would advise you to review your whole type system...
—SA

1 solution

You need to use the onselectedindexchanged event to select your new category and then run your add animal from there.
 
Share this answer
 
Comments
leprechauny 3-Feb-13 16:27pm    
Ye, I assumed that would be part of it. And it seems to work. However, how should I successfully bind it - as you can see the first list is called lstCategory and the second is lstAnimalType - and I can't seem to figure it out. The 'Enum.Parse' just doesn't do it for me. Any advice?
milenalukic 3-Feb-13 16:57pm    
I would first load the data source for list category and populate this list. Then on selected index changed run the data source for the animal with the selected category and populate the animal list.

Normally I would use list.items.add to populate the lists.

For(int i=0;i
leprechauny 3-Feb-13 18:41pm    
thanks a lot!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900