Filling Combobox from the list

Newb is here. I'm currently working on a form that has a combo box that shows some of Charlie Brown TV's special offers that you can click to select and view a description, rating, lead time, etc. I'm close, but I'm not there in terms of filling in the combo box, and I hope for help and guidance. I looked at a few things others did, but I'm not knowledgeable enough to get the answers out of what I have been able to see so far.

Now I'm also trying: 1. Get listings from your download method 2. Swipe through them 3. Enter the list box to fill out the field with time from the list.

Form1.cs

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Globalization;//Used for Sting.ToUpperCase... using System.Threading; using System.Threading.Tasks;// Needed for Streaming... using System.IO;// Needed for Streaming... namespace a100___GUI___VideoStoreSelections { public partial class FormMovieLookUp : Form { private const String FILE_NAME = "txt_movieDescriptions.txt";//connect to text file in debug private List<Listing> films { get; set; } public FormMovieLookUp() { InitializeComponent(); } private void cmbMovieListingBox_SelectedIndexChanged(object sender, EventArgs e) { txtTitleBox.Text = cmbMovieListingBox.SelectedItem.ToString(); } //ToolBox -- my program specific tools public List<Listing> LoadListings()//load movie descriptions as list { StreamReader fileIn = new StreamReader(FILE_NAME); List<Listing> entries = new List<Listing>(); //loop through every line of the file while (!fileIn.EndOfStream) { String line = fileIn.ReadLine(); String[] pieces = line.Split(':'); if (pieces.Length < 4) continue;//error handling - set to length of text items Listing myListing = new Listing(pieces[0], pieces[1], pieces[2], pieces[3]); entries.Add(myListing); } fileIn.Close(); return entries; } private void FormMovieLookUp_Load_1(object sender, EventArgs e) { films = LoadListings(); foreach (Listing film in films) { Console.WriteLine(film); cmbMovieListingBox.Items.Add(film.GetFilmTitle()); } } } } 

Listing.CS

 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace a100___GUI___VideoStoreSelections { public class Listing { private String filmTitle; private String description; private String filmRunTime; private String filmRating; public Listing(String filmTitle, String description, String filmRunTime, String filmRating) { this.filmTitle = filmTitle; this.description = description; this.filmRunTime = filmRunTime; this.filmRating = filmRating; } public String GetFilmTitle() { return filmTitle; } public String GetDescription() { return description; } public String GetFilmRunTime() { return filmRunTime; } public String GetFilmRating() { return filmRating; } } 

}

So here is what I'm trying to do to populate the combo box. Any help is fortunately received.

+7
list c # class combobox populate
source share
5 answers

I would keep the List<Listing> at the class level so that you can access it when the user clicks on it. I would also throw this on my own thread, and not directly into the Load event. If this is a long process, you hang ui.

 private List<Listing> films { get; set; } 

load

 films = LoadListings(); foreach (Listing film in films) { cmbMovieListingBox.Items.Add(film.GetFilmTitle()); } 

When the user selects an item

 Listing film = films.Where(f => f.GetFilmTitle().Equals(cmbMovieListingBox.SelectedValue)).FistOrDefault(); if (film != null) { //do work } 
+12
source share

If you ask what I think, you ask, you need something like this in your form:

 foreach(Listing listing in LoadListings()){ cmbMovieListingBox.Items.Add(listing.GetFilmTitle()); } 
+4
source share

There is one problem with updating visual controls (for example, ComboBox, etc.): you prefer not to redraw them every time the data changes (each time an element is added in your case):

 cmbMovieListingBox.BeginUpdate(); // <- Stop painting try { // Adding new items into the cmbMovieListingBox foreach(var item in LoadListings()) cmbMovieListingBox.Items.Add(item.GetFilmTitle()); finally { cmbMovieListingBox.EndUpdate(); // <- Finally, repaint if required } 
+1
source share

Tsukasa's line of code does not work because it is written by FistOrDefault () instead of FirstOrDefault ()

 Listing film = films.Where(f => f.GetFilmTitle().Equals(cmbMovieListingBox.SelectedValue)).**First**OrDefault(); 

Sorry, I don’t have enough points to just add a comment ...

+1
source share

Maybe this will help someone. But in my situation, I had to use cmbMovieListingBox.Text instead of cmbMovieListingBox.SelectedValue (e.g. @Tsukasa example):

 Listing film = films.Where(f => f.GetFilmTitle().Equals(cmbMovieListingBox.Text)).FirstOrDefault(); if (film != null) { //do work } 

And also FirstOrDefault() instead of FistOrDefault() .

Hope this helps someone.

0
source share

All Articles