如何显示指向ListBox中列表的对象?

时间:2016-08-23 23:28:31

标签: c# listbox

我有一个对象列表我想找到一种显示信息的方法,特别是我想知道我是否可以使用列表框来完成此操作。我弄清楚如何做到这一点的问题是我有一个对象列表,它有多个我想要显示的属性,其中一个是对象里面的列表,如下所示:

public  class Records
{
   //I may want to think about adding a way to keep track of what book number the record is actually a part of
   //rather than keeping that with the book title. that way it can be easier to reference.

 public  class Records
{
 public String Title { get; set; }
 public String FirstName { get; set; }
 public string LastName { get; set; }
 public List<int> Pages { get; set; }
 public string person { get; set; }
 public string Tag { get; set; }
 public int BookNumber { get; set; }
 public string Date { get; set; }
 private Type _type;
 public Type type
 {
     get
     {
       return this._type;
     }

     set
     {
         this._type = value;
         GrabDate();
     }
 }
}

由于记录中的页面列表,我如何使用列表框显示它?使用datagridview会更好吗?

2 个答案:

答案 0 :(得分:1)

您可以添加&#34;查看页面列表&#34;在列表框中代替实际列表,然后使用ListBox.OnSelectedIndexChanged来检测您的用户是否已选择它。如果他们选择了视图列表选项,则会执行某些操作来显示页面列表。

答案 1 :(得分:1)

您可以尝试以下两种方法。您可能还想引用this SO question,其中接受的答案建议使用DataGridView

第一次使用ListBox,绝对不是首选方法

第二种方式,我建议使用ListView,看起来好多了

<强>列表框

var recordList = BuildRecords();

foreach (var item in recordList)
{
    // To distinguish between 'pages' and other properties
    var gap = "    ";

    listBox1.Items.Add(item.Title);
    listBox1.Items.Add(item.FirstName);
    listBox1.Items.Add(item.LastName);

    foreach (var page in item.Pages)
    {
        listBox1.Items.Add(gap + page);
    }

    listBox1.Items.Add(item.person);
    listBox1.Items.Add(item.Tag);
    listBox1.Items.Add(item.BookNumber);
    listBox1.Items.Add(item.Date);
}

enter image description here

<强>的ListView

var recordList = BuildRecords();

var header1 = listView1.Columns.Add("Title", -2, HorizontalAlignment.Left);
var header7 = listView1.Columns.Add("firstname", -2, HorizontalAlignment.Left);
var header6 = listView1.Columns.Add("lastname", -2, HorizontalAlignment.Left);
var header5 = listView1.Columns.Add("pages", -2, HorizontalAlignment.Left);
var header4 = listView1.Columns.Add("person", -2, HorizontalAlignment.Left);
var header3 = listView1.Columns.Add("tag", -2, HorizontalAlignment.Left);
var header2 = listView1.Columns.Add("booknumber", -2, HorizontalAlignment.Left);
var header8 = listView1.Columns.Add("date", -2, HorizontalAlignment.Left);

foreach (var record in recordList)
{
    var lvi = new ListViewrecord(new[] {
        record.Title,
        record.FirstName,
        record.LastName,
        record.Pages[0].ToString(),
        record.person,
        record.Tag,
        record.BookNumber.ToString(),
        record.Date});

    listView1.Items.Add(lvi);

    for (int i = 1; i < record.Pages.Count-1; i++)
    {
        var lvi2 = new ListViewItem(new[] {
        string.Empty,
        string.Empty,
        string.Empty,
        record.Pages[i].ToString(),
        string.Empty,
        string.Empty,
        string.Empty,
        string.Empty});

        listView1.Items.Add(lvi2);
    }
}

enter image description here