Oh that's just a regular text file.
In the text file I type a movie name,rating,price, and showtime I did this for four movies. I wanted to use a combo box to list the movie name and uneditable fields for the rest.
I don't know where you're having trouble, but here is how I would approach the problem. (I have done winform programming, but I don't claim to be good at it)
Make a data type (class) for your movies. Call it Movie. It should be pretty basic; just have four public string fields for the name, rating, price and showtime.
Make a class whose job is to take a single line of text from the file and transform it into a Movie object. Call it something like MovieMapper. It will have a single method like: public Movie Map(string line). In other words, it will take a line of text, make a new Movie object, and set the movie's properties to the correct values from the string (probably using string.Split(',') I imagine? You're probably already doing this.
Make a class whose job is reading the text file and returning a list of Movie objects. Maybe call it MovieLoader, and give it a single method like:
Code:
public ICollection<Movie> LoadMovies(string filename)
It would work like this:
1) Read all the text from the file: File.ReadAllLines(filename)
2) For each line of text, use the MovieMapper to transform that line of text into a Movie.
3) Put all the movies into a list and return them.
Now, you have a class (MovieLoader) who takes a filename and gives you back all the movies. This maybe wasn't your problem before, but putting each separate problem into its own space gives you room to reason about things. Now you can grab all the movies with a single line of code:
Code:
var movies = movieLoader.LoadMovies(@"C:\whatever\movies.txt");
Or better yet, you can also add them all to the combobox in that line too:
Code:
comboBox1.Items.AddRange(movieLoader.LoadMovies(@"C:\whatever\movies.txt");
Next you want only the name of the movie to show in the combobox right? It looks like by default the text ComboBox shows for an item is whatever is returned by that item's ToString() method:
http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.items.aspx
Alternatively, you can tell the combobox which property to use by setting the combobox's DisplayMember property.
So, add a ToString() method to Movie which just returns the movie's name. Like:
Code:
public override string ToString()
{
return this.Name;
}
Now if you add a Movie to a combobox, the box will only show the movie's name.
Now to popular other text fields with other information about the movie, you want to take action whenever the user changes the combobox's selection. Register a handler for the combobox's
SelectedValueChanged event. You can either do this manually with a line of code, or just go to the combobox's property window, filter by events, find SelectedValueChanged and double click on it to generate a method.
In your combobox's selected value changed event handler method, simply grab the current selection, cast it to a Movie, and fill your text boxes with the selected movie's properties. Something like this:
Code:
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
var selectedMovie = (Movie)comboBox1.SelectedItem;
textBox1.Text = selectedMovie.Price;
textBox2.Text = selectedMovie.Rating;
textBox3.Text = selectedMovie.Showtime;
}