1
\$\begingroup\$

I got this answer here: https://stackoverflow.com/questions/15902299/selecting-an-item-in-a-listbox-to-assign-it-as-a-temp-variable


Suppose you have loaded the lisBox1 with objects of type Genre.

lisBox1.Datasource = this.GetAllGenresFromTextFiles();
lisBox1.DisplayMember = "Genre";

Add an event to SelectionChanged of your lstBox1 :

lstBox1.SelectionChanged += lstBox1_SelectionChanged;

And then in this event, filter the data for listbox2 :

void lstBox1_SelectionChanged(object sender, EventArgs e)
{
    var movies = m_allMovies;
    var genre = lstBox1.SelectedItem as Genre;
    if(genre != null)
    {
        movies = movies.Where(m => m.Genre == genre.Genre).ToList();
    }
    lstbox2.Datasource = movies;
}

m_allMovies is a list of all the movies you have loaded from your textfiles.
You can also read your text file every time the user clicked a different genre, it will consume less memory but more CPU:

void lstBox1_SelectionChanged(object sender, EventArgs e)
{
    var movies = this.GetAllMoviesFromTextFiles();
    var genre = lstBox1.SelectedItem as Genre;
    if(genre != null)
    {
        movies = movies.Where(m => m.Genre == genre.Genre).ToList();
    }
    lstbox2.Datasource = movies;
}

The keypoint here is not to filter row from the Listbox. You must think of it as your Listbox contains a subset of all your movies.
And each time you want to add or remove rows from a Listbox, in fact you are reloading it with appropriate datas.


and my problem is this:

With the first 2 lines of code (At the top of the answer), where would i place this (I'm new to C# so .Datasource & DisplayMember are new things to me). Also where you have "this.GetAllGenresFromTextFiles" (and the same for var movies = this.GetAllMoviesFromTextFiles) --> would i place the location of my text file within the brackets?

\$\endgroup\$
1
\$\begingroup\$

Not sure if this question really fits here, but I'll try to help anyway.

With the first 2 lines of code (At the top of the answer), where would i place this (I'm new to C# so .Datasource & DisplayMember are new things to me).

DataSource is used when you're databinding a control to a collection.
DisplayMember is used to determine which member (property) of the databound type will be used. For instance you have a class Person (id, firstname, lastname) and you want to display lastnames in your listbox. So you set listbox.DisplayMember = "lastname"

You can put these lines pretty much anywhere. When you fire up the method containing these commands, your listbox will get populated by the data specified in DataSource. You can use Load event for instance, however, I'd recommend to use an extra method called PopulateListBox and call that from Load event...

would i place the location of my text file within the brackets?

That depends on many things..For instance what exactly are you trying to achieve, how is the rest of your application structured, what are you programming habits etc...
If you wanted to use the code from Scorpi0, the method GetAllMoviesFromTextFiles expects to have everything it needs without any input. It should contain a method body to get all movies.

Furthermore, I'd suggest you one thing - if this is too advanced for you and you still don't understand, get some books and start with something simpler. You need to understand how to divide the application into logical layers, how to separate UI from data access etc. This is a topic for multiple books and a few years experience. You can't expect just to start typing some code and immediately get it all right. From a big part it's very opinionated as well, there are rarely "the best practices". You ask 10 different programmers and you get 10 different answers. You need to learn many things, so you will be able to decide for yourself what suits you the best in the current situation you're in.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.