我希望能够在列表中创建一系列对象,然后在该列表中搜索特定结果(可能有 0、1 或许多命中),然后在文本框中显示结果。
这是我到目前为止所拥有的,但我似乎只能显示我输入的最后一个对象。
public partial class OrganiserWindow : Form
{
public OrganiserWindow()
{
InitializeComponent();
}
List<Album> AlbumList = new List<Album>();
private void createAlbum_Click(object sender, EventArgs e)
{
AlbumList.Add(new Album(albumBox.Text, artistBox.Text));
}
private void searchAlbum_Click(object sender, EventArgs e)
{
var albumResult = from album in AlbumList
where album.AlbumName != null// == albumBox.Text
select new { Name = album.AlbumName, Artist = album.ArtistName };
foreach (var item in albumResult)
{
albumResultBox.Text = String.Join(Environment.NewLine, item.Name + " " + item.Artist);
}
}
}
public class Album
{
private string albumName;
private string artistName;
public Album(string album, string artist)
{
albumName = album;
artistName = artist;
}
public string AlbumName
{
get
{
return albumName;
}
set
{
albumName = value;
}
}
public string ArtistName
{
get
{
return artistName;
}
set
{
artistName = value;
}
}
}