Populate Combobox from a list

Newb here, I'm currently working on a form which has a combo box, which will show several Charlie Brown TV specials which you can click on to select and see a description of, rating, runtime, etc. I'm close but I'm not there in terms of populating the combo box and i'm hoping for some help and guidance. I have looked at several things others have done but i'm not knowledgeable enough to deduce the answers from what i've been able to see so far.

Right now i'm trying too: 1. get the listings from your load method 2. loop through them 3. Access my combo box to populate the box with the times from the listing.

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 this is what i'm trying to do to populate my combo box. Any help is thankfully received.

I would hold List<Listing> at the class level so you can access it when a user clicks on it. I would also throw this on it's own thread and not directly in the Load event. If it's a long process you will hang the ui.

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

Load

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

When the user selects the item

Listing film = films.Where(f => f.GetFilmTitle().Equals(cmbMovieListingBox.SelectedValue)).FistOrDefault();

if (film != null)
{
    //do work
}

Populating a combobox with List<string>, I think AddRange is the method you're looking for //Assuming you don't want to continually add new items use Clear() cbSelectEditName.Items.Clear(); //Use� Open the Visual Basic Editor (VBE) by pressing [Alt]+ [F11]. From the Insert menu, choose UserForm. Using the Toolbox, insert a combo box control. The Toolbox should be visible when you select the userform. If necessary, I added a label control and entered the text Color. You can skip this step

if you are asking what i think you are asking, you need something like this in your form load:

foreach(Listing listing in LoadListings()){
    cmbMovieListingBox.Items.Add(listing.GetFilmTitle());
}

How to Populate ComboBox on VBA Userforms, With the . List property. Create the UserForm. Populate the VBA Combobox. Accessing the Initialization Event. Populate ComboBox with .List. Populate ComboBox with a Range. Populating Multi-Column ComboBox. Populate ComboBox with .AddItem. Add Item to Different Positions in ComboBox. Conclusion. VBA code: Populate Combo box with data of a named range. Private Sub ComboBox1_Change()'Updated by Extendoffice 2018/1/30 Dim xRg As Range Set xRg = Range(Me.ComboBox1.Text) Me.ComboBox2.List = Application.WorksheetFunction.Transpose(xRg)End SubPrivate Sub Worksheet_SelectionChange(ByVal Target As Range) Dim xRg As Range Set xRg = Range("Headers") Me.ComboBox1.List = Application.WorksheetFunction.Transpose(xRg)End Sub.

There's one issue with visual controls updating (such as ComboBox etc): you'd rather prevent them from re-painting at each data change (at each item addition 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
}

Populate Combo Box From List in A different Class - MSDN, I'm currently using the following code to populate the combobox but it doesn't seem to work? Any ideas? Im still pretty new to C#. private void� Populate Userform ListBox or ComboBox(A Microsoft Word Help & Tip page by Gregory K. Maxey) 1. See my Create & Employ a Userform tips page for information on creating and employing userforms. 2. The basic process for populating a listbox or combobox is the same. For brevity, I will use one term or

A line of the code of Tsukasa doesn't work because it is written FistOrDefault() instead of FirstOrDefault()

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

Sorry I don't have enough point to just add a comment...

Programmatically Binding DataSource To ComboBox In Multiple Ways, Now we want to populate a ComboBox with the person's name and when the user selects a name on the list, his/her corresponding gender is� The best method to fill a Combobox/listbox is the method 'List'. You can directly assign any 1-dimensional or 2-dimensional array to a combobox/listbox. The nice thing is that the combobox/listbox automatically preserves the 'rows' and 'columns' of the array.

Maybe it will help somebody. But in my situation I had to use cmbMovieListingBox.Text instead of cmbMovieListingBox.SelectedValue (like @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 it helps to someone

VBA for smarties How to fill a Combobox / Listbox, That's probably due to the fact that the new item has to find it's position in the existing list. So 'Additem' isn't meant to populate a combobox/listbox. How to fill Combo box from JAVADB. 2. How do I populate a combobox with an observableArray?-1. Select from a UI list and perform an action. 1.

[Resolved] Populate ComboBox from a List, Hi, I have a listnow i want to bind that to a combo boxBut i couldn't do it [CODE]List<string> IndiaState = new List<string>() Binding a ComboBox to an Enum in VB.Net. Enum provide a better way in which to create a group of related constants that can be grouped as strings or integer values. When these values are bind to a ComboBox, the end user can select an option directly from a list of the Enum strings. Data Binding an Enum with Descriptions

C# ComboBox - CSharp, A ComboBox displays a text box combined with a ListBox, which enables the user to select items from the list or enter a new value. ComboBox DataSource Property. How to populate a combo box with a DataSet ? You can Programmatically� I am trying to populate a combobox with a range of cells from another sheet in my workbook (called "Other"). I use the following as a guide, but it is not seeming to work. Can anyone offer me advice? When i run the userform, the combobox is not populated with anything.

C# Tutorial, How to populate a ComboBox with data in C#. The C# Basics beginner course is a free C Duration: 4:11 Posted: Jul 27, 2016 Another possible way to populate a ComboBox is to let a user do it. A ComboBox can be linked to the cells range. In this approach, every time a user enters a new value in the cells range, the ComboBox will update with that value.

Comments
  • Your code already uses loops and accesses the combo box. What exactly is keeping you from getting the list, looping over it and adding each elements text to the combobox?
  • When i run it (click the green arrow to compile/test) the box doesn't populate. So that suggests to me that i've no idea of what I'm doing. Generally speaking, as i'm a newb, i can tell you i have no idea of what i'm doing. I was hoping the combo box would populate with the first piece from each line in my text document.
  • I will try this out and report back. Thank you
  • I tried this but i was not able to make it work. It seems like it would work but for me it did not. I thank you for your assistance. You are awesome.
  • Can you post your code with this so I can look and see why it wasn't working for you.
  • I've updated the code to show my latest attempt to implement your suggestion. I put all the code into the form1.cs file.Errors happen on lines 69: 'film' can't be declared as it would change film used in child; Error 2 'System.Collections.Generic.IEnumerable<a100___GUI___VideoStoreSelections.Listing>' does not contain a definition for 'FistOrDefault' and no extension method 'FistOrDefault' accepting first argument of type 'System.Collections.Generic.IEnumerable<a100___GUI___VideoStoreSelections.Listing>' could be found (are you missing a using directive/assembly reference?)
  • I don't know what exactly, I went over this with my teacher who said that yes it ought to work. She gave the file a debug zap or something like that. Open and closed it a few times and then it worked. She said the issue was that the last method simply wasn't running - we aren't sure why it wasn't but it just self started when we changed computers. Now its' good. Sort fo a mystery I guess.
  • Thank you - i Will try this and report back.
  • I tried this but was not able to get it to work. I thank you for your assistance - you are also awesome.
  • I tried this too, sadly it produced many errors. It looks really smart but i think it is in my attempt to implement it that the errors came wheni put it into the private void FormMovieLookUp_Load(object sender, EventArgs e) method. Thanks. I see what this does, forms are very vexing things