C# 搜索的项目不能添加到列表框中

C# 搜索的项目不能添加到列表框中,c#,listbox,listboxitems,C#,Listbox,Listboxitems,在搜索功能中搜索的项目:相册不会添加到列表框中吗? 其他字段填充 您能告诉我如何用搜索到的相册填充列表框吗 相册可以使用链表查找 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks;

在搜索功能中搜索的项目:相册不会添加到列表框中吗? 其他字段填充

您能告诉我如何用搜索到的相册填充列表框吗 相册可以使用链表查找

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Assignment
{
public partial class frmAddArtist : Form
{
    AVLTree<Artist> avltree = new AVLTree<Artist>();
    LinkedList<Album> temp = new LinkedList<Album>();
    Artist artistinst;
    Album albuminst;
    string noofmembers, artistname;
    int artistcount;

    public frmAddArtist()
    {
        InitializeComponent();
    }
    private void label5_Click(object sender, EventArgs e)
    {

    }

    private void btnAddArtist_Click(object sender, EventArgs e)
    {
        string tempalbum, date;
        tempalbum = txtAlbumName.Text;
        date = dtpReleaseDate.Text.ToString();
        albuminst = new Album(tempalbum, date);
        temp.AddFirst(albuminst);
        lbAlbums.Items.Add(tempalbum);
    }

    private void btnSave_Click(object sender, EventArgs e)
    {

        artistname = txtArtistName.Text;
        noofmembers = txtNoOfMembers.Text;
        artistinst = new Artist(artistname, noofmembers, temp);
        avltree.InsertItem(artistinst);
        artistcount++;
        txtArtistName.Clear();
        txtNoOfMembers.Clear();
        txtAlbumName.Clear();
        lbAlbums.Items.Clear();
        temp.Clear();
    }

    private void btnNoOfArtist_Click(object sender, EventArgs e)
    {
        MessageBox.Show("The No. Artist: " + Convert.ToString(artistcount)); 
    }

    private void btnHeight_Click(object sender, EventArgs e)
    {
        int heightoftree = avltree.Height();
        string height = Convert.ToString(heightoftree);
        MessageBox.Show("The Height of the Tree: " + height);
    }

    private void btnSearch_Click(object sender, EventArgs e)
    {
        Artist temp = new Artist(txtSearch.Text, " ", null);
        Artist result = avltree.Search(temp);
        if (result != null)
        {
            if (result.CompareTo(temp) == 0)
            {
                txtArtistName.Text = result.artistname;
                txtNoOfMembers.Text = result.noofmembers;
                foreach (Album p in result.Albumslist)
                {
                    lbAlbums.Items.Add(p.Albumname);
                }
            }
                else if(result.CompareTo(temp) <0)
                {
                    MessageBox .Show("No Match Found");
                } 

            }
        }
    }
}

将项目放入LinkedList或列表中。
然后设置lbAlbums.ItemsSource=

相册已在链接列表公共LinkedList Albumslist{get{return Albumslist;}集中{albumslist=value;}}然后只需将ListBox的ItemsSource设置为List,一切都将正常进行;您必须覆盖相册的ToString方法,否则您将只看到namespace.Album。