What is the best way to solve this problem

T

Tony Johansson

Hello!

There are two listboxes in the first one I have differend kind of animal
category such as Bird,Mammal,Reptile and the second one should show all
those animal type that correspond to the animal category.
An example if I choose Mammal in the category listBox then in the animal
type listbox should display Bear,Cat,Deer,Lion,Dog,Horse,Panda and Wolf.

So when I choose a category only the animal that belong to the selected
animal category should be displayed in the animal type listBox.

What is the best kind of solution and what class is most suitable here ?

//Tony
 
J

Jeff Gaines

If you are trying to comply with some certification requirement, exam,
whatever, then asking the question here is cheating. :p

It has been interesting to watch Tony's development through the group.
When he gets his qualification I'm sure he'll dedicate it to you and other
notable posters :)
 
T

Tony Johansson

Peter Duniho said:
Impossible to say given your description. A lot of the specifics depend
on where the data comes from, and how you're using it elsewhere in the
program.

What I can tell you is that you'll have two ListBox instances (most likely
added to the parent control or form with the Designer), you'll respond to
one of the selection-changed events in the first ListBox, and then clear
and repopulate the second ListBox based on the new selection.

If you are just trying to get the job done, worrying about the _best_ kind
of solution is not good use of your time. Just get _a_ solution that
works well enough, and then improve it if necessary.

If you are trying to comply with some certification requirement, exam,
whatever, then asking the question here is cheating. :p

Pete

Here I have a working solution that is ok.
But there is one thing in this solution that I don't like and that is that I
have to use hard coded string for the
animal type class. I can't see any suitable way to find out which class I
have as animal type.
In this example below I have tvo classes for each animal category.
In the Init method I fill the collection with a category and the animal type
that I have as you can see.
In method GetMyValues I pass in the key for my collection and then a return
a List<T> for the values that match the key.

I hate use hard coded string that I have to use here.
This problem has nothing to do with any type of certification.
All certification test is done at a specialist company that is allowed to
give these kind of test.

class Category
{
private NameValueCollection nvc = new NameValueCollection();

public Category()
{
Init();
}

private void Init()
{
foreach (string value in Enum.GetNames(typeof(CategoryType)))
{
if (value == CategoryType.Bird.ToString())
{
nvc.Add(value, "Duck");
nvc.Add(value, "Kolibri");
}
else if (value == CategoryType.Insect.ToString())
{
nvc.Add(value, "Butterfly");
nvc.Add(value, "Bee");
}
else if (value == CategoryType.Mammal.ToString())
{
nvc.Add(value, "Cat");
nvc.Add(value, "Bear");
}
else if (value == CategoryType.Marine.ToString())
{
nvc.Add(value, "Pike");
nvc.Add(value, "GoldFish");
}
else if (value == CategoryType.Reptile.ToString())
{
nvc.Add(value, "Frog");
nvc.Add(value, "Snake");
}
}
}

public List<string> GetMyValues(string key)
{
List<string> returnValueList = new List<string>();
foreach(string s in nvc.GetValues(key))
{
returnValueList.Add(s);
}

return new List<string>(returnValueList);
}
}

//Tony
 
K

kndg

Hello!

There are two listboxes in the first one I have differend kind of animal
category such as Bird,Mammal,Reptile and the second one should show all
those animal type that correspond to the animal category.
An example if I choose Mammal in the category listBox then in the animal
type listbox should display Bear,Cat,Deer,Lion,Dog,Horse,Panda and Wolf.

So when I choose a category only the animal that belong to the selected
animal category should be displayed in the animal type listBox.

What is the best kind of solution and what class is most suitable here ?

//Tony

This kind of problem can easily be solved with data binding. If you hate
to hard-code your data, you can load it from xml file. Example below (I
assume you already put two listboxes, named listBox1 & listBox2, on your
form)

public partial class Form1 : Form
{
private List<AnimalCategory> animals;
private readonly BindingSource animalListBinding = new BindingSource();

public Form1()
{
InitializeComponent();
ReadData();
InitListBox();
}

private void ReadData()
{
using (Stream stream = File.OpenRead("Animals.xml"))
{
var serializer = new XmlSerializer(typeof(List<AnimalCategory>));

try
{
animals = (List<AnimalCategory>)serializer.Deserialize(stream);
}
catch (InvalidOperationException)
{
MessageBox.Show(@"Unable to load data file. Application will
now exit\n");
Application.Exit();
}
}
}

private void InitListBox()
{
animalListBinding.DataSource = animals;

listBox1.DataSource = animalListBinding;
listBox1.DisplayMember = "Name";

listBox2.DataSource = animalListBinding;
listBox2.DisplayMember = "Animals.Name";
}
}

public class Animal
{
[XmlText]
public string Name { get; set; }
}

public class AnimalCategory
{
[XmlAttribute]
public string Name { get; set; }
[XmlElement("Animal")]
public List<Animal> Animals { get; set; }
}

Animals.xml

<?xml version="1.0"?>
<ArrayOfAnimalCategory
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AnimalCategory Name="Bird">
<Animal>Duck</Animal>
<Animal>Kolibri</Animal>
</AnimalCategory>
<AnimalCategory Name="Insect">
<Animal>Butterfly</Animal>
<Animal>Bee</Animal>
</AnimalCategory>
<AnimalCategory Name="Mammal">
<Animal>Cat</Animal>
<Animal>Bear</Animal>
</AnimalCategory>
<AnimalCategory Name="Marine">
<Animal>Pike</Animal>
<Animal>GoldFish</Animal>
</AnimalCategory>
<AnimalCategory Name="Reptile">
<Animal>Frog</Animal>
<Animal>Snake</Animal>
</AnimalCategory>
</ArrayOfAnimalCategory>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top