PC Review


Reply
Thread Tools Rate Thread

What is the best way to solve this problem

 
 
Tony Johansson
Guest
Posts: n/a
 
      27th Jan 2011
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


 
Reply With Quote
 
 
 
 
Jeff Gaines
Guest
Posts: n/a
 
      27th Jan 2011
On 27/01/2011 in message
<(E-Mail Removed)> Peter Duniho
wrote:

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


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 :-)

--
Jeff Gaines Wiltshire UK
There is no reason anyone would want a computer in their home.
(Ken Olson, president Digital Equipment, 1977)
 
Reply With Quote
 
Tony Johansson
Guest
Posts: n/a
 
      27th Jan 2011
"Peter Duniho" <(E-Mail Removed)> skrev i meddelandet
news:(E-Mail Removed)...
> On 1/27/11 10:20 PM, Tony Johansson wrote:
>> 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 ?

>
> 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.
>
> 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


 
Reply With Quote
 
kndg
Guest
Posts: n/a
 
      28th Jan 2011
On 1/27/2011 10:20 PM, Tony Johansson wrote:
> 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>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to solve this problem using .NET jeroen Microsoft Dot NET 1 30th Sep 2004 09:50 PM
Problem when multipple users access shared xl-file at the same time, macrocode for solve this problem? OCI Microsoft Excel Programming 0 16th May 2004 10:40 PM
Please, help me to solve this problem Mat Microsoft VB .NET 7 10th Nov 2003 12:00 PM
these URLs helped me solve my bridging problem...hope it can helps someone else and save the 3 days it took me to solve kenw Windows XP Networking 0 25th Jul 2003 06:21 PM
Re: can anyone solve my problem? who me? Windows XP Internet Explorer 0 4th Jul 2003 04:14 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:59 AM.