• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

Populating a list box on a windows form in C#

I have been able to pass a variable(Genre) to a windows form in C#.  With that variable I call another class which uses that variable to return a list of movie titles from an Access database based on the genre it is in. The correct movie titiles are being returned, but when I try and pass them to the method that populates the form's listbox, nothign happnes.  Here is my code for the form, please let me know if you see anything I can do to fix my problem.  I also attached the file.  I will also attach the data acces files and the classes this form utilizes.
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace FinalProject
{
    public partial class BrowseTitle : Form
    {        
        //Declare variables
        string title = "";
        string genre = "";
        Movie aMovie = new Movie();
        Genre aGenre = new Genre();        
        public ArrayList allTheMovies = new ArrayList();
        
        public BrowseTitle(string newGenre)
        {            
            InitializeComponent();
            aGenre.initializeConnection();
            allTheMovies = Genres.allMovies(newGenre);   <---- here, allTheMovies contains the correct movie objects
            aMovie = (Movie)allTheMovies[0];
            //aGenre = (Genre)allTheMovies[0];
            SetMovieList(aMovie);		<--------------- aMovie is the movie object in position 0 in allTheMovies
        }
 
        public string Genre
        { 
            get { return genre; }
            set { genre = value; }
        }        
 
        private void MovieInfoBtn_Click(object sender, EventArgs e)
        {
            //Declare variable
            title = MovieTitleList.SelectedItem.ToString();
            
            //Make Movie Info the active form
            MovieInfo movInfo = new MovieInfo(title);
            this.Hide();
            movInfo.Show();
        }
 
        private void BackBtn_Click(object sender, EventArgs e)
        {
            //Make Browse Genre the active form
            BrowseGenre bGenre = new BrowseGenre();
            this.Close();
            bGenre.Show();
        }
 
        private void RentBtn_Click(object sender, EventArgs e)
        {
            //Declare variable
            title = MovieTitleList.SelectedItem.ToString();
 
            //Pass selection to RentMain
 
            //Make Rent Main the active form
            RentMain rentM = new RentMain();
            this.Close();
            rentM.Show();
        }
 
        private void SetMovieList(Movie aMovie)		<-------------- aMovie still holds the movie object
        {               
            //Movie newMovie = new Movie();                     >               
            this.MovieTitleList.Items.Clear();			>---------------------- Nothing is being passed to these
            foreach (Movie movieTitle in aGenre.AllMovies)	>
                this.MovieTitleList.Items.Add(movieTitle);	>
        }
    }
}

Open in new window

form.txt
0
udeeze
Asked:
udeeze
  • 2
1 Solution
 
abelCommented:
You are passing a single movie to a method that (from the name of it and its contents) should actually receive the whole list. Why are you only passing only a single movie? From that single movie, you can never enter the other movies in the list anymore...

I don't see the definition of this.MovieTitleList, is that the ListBox? You reference aGenre, but it is not being declared in the scope. Maybe you mean this:

private void SetMovieList(ArrayList movieList) 
{               
    this.MovieTitleList.Items.Clear();   
    foreach (Movie aMovie in movieList)
    {
                this.MovieTitleList.Items.Add(aMovie.Title);
    }
}

Open in new window

0
 
udeezeAuthor Commented:
Thank you, that worked great.  I appreciate your quick response.
0
 
abelCommented:
> Thank you, that worked great.  I appreciate your quick response.

glad it helped. I was a bit puzzled by what you meant, but apparently hit it at the right spot ;-)
0

Featured Post

NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now