gpt4 book ai didi

c# - 从列表中填充组合框

转载 作者:可可西里 更新时间:2023-11-01 08:55:42 26 4
gpt4 key购买 nike

新人在这里, 我目前正在处理一个带有组合框的表格,它将显示几个查理布朗电视特别节目,您可以单击以选择并查看描述、评级、运行时间等。我很接近但我不是在填充组合框方面,我希望得到一些帮助和指导。我看过其他人做过的几件事,但我的知识还不够丰富,无法从目前所看到的内容中推断出答案。

现在我也在尝试: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());
}
}
}
}

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



}

这就是我试图填充组合框的方法。谢天谢地收到任何帮助。

最佳答案

我会持有 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
}

关于c# - 从列表中填充组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20344378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com