从列表中填充Combobox

时间:2013-12-03 06:31:37

标签: c# list class combobox populate

Newb在这里,    我目前正在制作一个有一个组合框的表格,它将显示几个查理布朗电视特价,你可以点击选择并查看描述,评级,运行时等。我很接近,但我不是在填充组合框方面,我希望得到一些帮助和指导。我已经看过别人做过的几件事,但是我不够了解到目前为止我所能看到的答案。

现在我也在尝试: 1.从您的加载方法获取列表 2.循环它们 3.访问我的组合框,使用列表中的时间填充该框。

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; }



}

}

所以这就是我想要填充我的组合框。谢天谢地收到了任何帮助。

6 个答案:

答案 0 :(得分:15)

我会在课程级别保留List<Listing>,以便您在用户点击它时可以访问它。我也会把它放在它自己的线程上,而不是直接在Load事件中。如果这是一个漫长的过程,你将挂起ui。

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

加载

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

当用户选择项目

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

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

答案 1 :(得分:4)

如果你问我认为你在问什么,你需要在表格中加载这样的东西:

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

答案 2 :(得分:1)

视觉控件更新存在一个问题(例如ComboBox等):您宁愿阻止它们在每次数据更改时重新绘制(在您的案例中添加每个项目时):

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
}

答案 3 :(得分:1)

Tsukasa代码的一行不起作用,因为它写的是FistOrDefault()而不是FirstOrDefault()

POST https://graph.windows.net/myorganization/{resource_collection}/{resource_id}/getMemberGroups?api-version
Content-Type: application/json
{
  "securityEnabledOnly": false
}

抱歉,我没有足够的意见只是添加评论...

答案 4 :(得分:0)

也许它会对某人有所帮助。但在我的情况下,我不得不使用cmbMovieListingBox.Text代替cmbMovieListingBox.SelectedValue(例如@Tsukasa示例):

Listing film = films.Where(f => f.GetFilmTitle().Equals(cmbMovieListingBox.Text)).FirstOrDefault();

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

FirstOrDefault()代替FistOrDefault()

希望对某人有帮助

答案 5 :(得分:0)

从列表声明中删除{get; set;}。那里不需要。

这样定义您的班级:

public class Listing
{
    private String filmTitle {get; set;}
    private String description {get; set;};
    …
}

在表单加载事件中,将ComboBox DisplayMemberValueMember设置为“ filmTitle”

cmbMovieListingBox.DisplayMember = "filmTitle";
cmbMovieListingBox.ValueMember = "filmTitle"

最后,您必须将DataSource中的ComboBox设置为列表

cmbMovieListingBox.DataSource = films;

就在那里。您的其余代码现在应该可以运行。