Alexandre Alexandre - 3 months ago 8
C# Question

How would I display an object that points to a list in a ListBox?

I have a list of objects I would like to find a way to display information for, specifically I want to know if I could do this using a listbox. My problem with figuring out how to do this is that I have a list of objects that have multiple properties that i would like to display, and one of them is a list inside the object like this:

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


because of the list of pages in the records, how would I display this using a Listbox? would it be better just to use a datagridview?

Answer

Here are 2 methods that you could try. You may also want to refer to this SO question where the accepted answer suggests using DataGridView

The first uses ListBox, and definitely doesn't seem to be the preferred method

The second way of doing it, which I will recommend uses ListView, which looks a lot nicer

ListBox

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