Patrick Boston
Patrick Boston

Reputation: 77

Remove a selected ListView item from an actual List

I want to be able to remove an item from a List by selecting it from a ListView and hitting a button to delete it. This button would remove it from both the actual List and the Listview. What I currently have removes the item from the Listview, but does not remove the Movie object from the actual movieList List. The List is located in another class called MovieRentalSys (movieSystem.movieList). Using WinForms.

partial class MovieRentalSys
{
    public List<Movie> movieList;
    public List<Movie> reservedMovieList;
    Movie movie;

    public MovieRentalSys()
    {
        movieList = new List<Movie>();
        reservedMovieList = new List<Movie>();
    }

    //Will take movie form text as arguments to create new movie in list
    public List<Movie> AddMovie(string newMovie, string newGenre, string newActor, string newYear)
    {
        movie = new Movie(newMovie, newGenre, newActor, newYear);
        movieList.Add(movie);
        return movieList;
    }

    public List<Movie> DeleteMovie(Movie movie)
    {
        //needs to be made
    }

    public List<Movie> ReserveMovie(Movie movie)
    {
        movieList.Remove(movie);
        reservedMovieList.Add(movie);
        return reservedMovieList;
    }
}



public partial class frmMovie : Form
{
    MovieRentalSys movieSystem = new MovieRentalSys();


    public frmMovie()
    {
        InitializeComponent();
    }

    private void btnMovieBack_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void btnAddMovie_Click(object sender, EventArgs e)
    {
        movieSystem.AddMovie(txtTitle.Text, txtGenre.Text, txtActor.Text, txtYear.Text);
        movieSystem.WriteMovieListToFile();
        ListViewItem movie = new ListViewItem(txtTitle.Text);
        movie.SubItems.Add(txtGenre.Text);
        movie.SubItems.Add(txtActor.Text);
        movie.SubItems.Add(txtYear.Text);
        lstMovies.Items.Add(movie);
    }

    private void btnDeleteMovie_Click(object sender, EventArgs e)
    {
        lstMovies.Items.Remove(lstMovies.Items[0]);
        //need method to remove movie from actual movieList
    }
}

Anyone know how to do this with a method, specifically with a method that can be called in this button?

Upvotes: 0

Views: 310

Answers (1)

Jason Watkins
Jason Watkins

Reputation: 3785

So, there are lots of things going on here. First, it doesn't make much sense for your MovieRentalSys methods to return a List. Instead, AddMovie should return the created Movie object.

public Movie AddMovie(string newMovie, string newGenre, string newActor, string newYear)
{
    movie = new Movie(newMovie, newGenre, newActor, newYear);
    movieList.Add(movie);
    return movie;
}

Now, when you create the ListViewItem, you can store the Movie reference in the item's Tag

private void btnAddMovie_Click(object sender, EventArgs e)
{
    Movie movie = movieSystem.AddMovie(txtTitle.Text, txtGenre.Text, txtActor.Text, txtYear.Text);
    movieSystem.WriteMovieListToFile();
    ListViewItem movieItem = new ListViewItem(txtTitle.Text);
    movieItem.SubItems.Add(txtGenre.Text);
    movieItem.SubItems.Add(txtActor.Text);
    movieItem.SubItems.Add(txtYear.Text);
    movieItem.Tag = movie 
    lstMovies.Items.Add(movieItem);
}

Finally, you can retrieve the Movie from the tag, cast it back to the correct type, and pass it to DeleteMovie

private void btnDeleteMovie_Click(object sender, EventArgs e)
{
    ListViewItem selectedItem = lstMovies.Items[0];
    lstMovies.Items.Remove(selectedItem);
    movieSystem.DeleteMovie((Movie)(selectedItem.Tag));
}

Removing items from Lists this way isn't very efficient though, so you would probably be better off using a Dictionary that stores movies based on their name.

partial class MovieRentalSys
{
    public Dictionary<string, Movie> movieList;

    ...

    //Will take movie form text as arguments to create new movie in list
    public Movie AddMovie(string newMovie, string newGenre, string newActor, string newYear)
    {
        movie = new Movie(newMovie, newGenre, newActor, newYear);
        movieList.Add(newMovie, movie);
        return movie;
    }

    public List<Movie> DeleteMovie(string movieName)
    {
        //needs to be made
    }

    ...
}

Now you can take the same approach in the form, but store only the movie name in the tag instead of the movie itself.

Upvotes: 1

Related Questions