我希望能够通过从ListView中选择项目并按下按钮将其删除,从列表中删除项目。此按钮将从实际List和Listview中删除它。我目前有什么从Listview中删除项目,但不从实际的movieList列表中删除Movie对象。 List位于另一个名为MovieRentalSys(movieSystem.movieList)的类中。使用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
}
}
任何人都知道如何使用方法执行此操作,特别是可以在此按钮中调用的方法吗?
答案 0 :(得分:1)
所以,这里有很多事情要发生。首先,返回List的MovieRentalSys
方法没有多大意义。相反,AddMovie
应该返回创建的Movie对象。
public Movie AddMovie(string newMovie, string newGenre, string newActor, string newYear)
{
movie = new Movie(newMovie, newGenre, newActor, newYear);
movieList.Add(movie);
return movie;
}
现在,当您创建ListViewItem
时,您可以将Movie
引用存储在项目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);
}
最后,您可以从代码中检索Movie
,将其转换回正确的类型,然后将其传递给DeleteMovie
private void btnDeleteMovie_Click(object sender, EventArgs e)
{
ListViewItem selectedItem = lstMovies.Items[0];
lstMovies.Items.Remove(selectedItem);
movieSystem.DeleteMovie((Movie)(selectedItem.Tag));
}
以这种方式从列表中删除项目并不是非常有效,因此您可能最好使用根据其名称存储电影的词典。
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
}
...
}
现在您可以在表单中采用相同的方法,但只在电影中添加电影名称而不是电影本身。