簡體   English   中英

在列表框中顯示多個列表

[英]Display mutliple lists in a list box

我正在使用c#Visual Basic 2017,並且正在嘗試創建一個列表框,該列表框將同時顯示3個不同的列表,並想知道是否有可能。 列表框當前可以通過使用“ .datasource =”函數來顯示每個列表。 每個列表都是類類型,這些類都是不同的,但是有一個超類和兩個子類。

這就是我在表單中命名並填寫列表的方式:

public partial class formHome : Form
{
    // Naming and creating my lists at class level so other classes such as buttons can access the lists
    List<Event> events = new List<Event>();
    List<Hospitality> hosps = new List<Hospitality>();
    List<Conference> confs = new List<Conference>();



    public formHome()
    {
        InitializeComponent();

        // Fill Event list
        events.Add(new Event(1, new DateTime(2017, 12, 21), new DateTime(2017, 12, 22), "National Glass Centre", 20, 7.50, "Dynamo Kick off Meeting, Dec 2017"));
        events.Add(new Event(2, new DateTime(2017, 12, 15), new DateTime(2017, 12, 16), "Winter Gardens Museum", 30, 2.99, "Nature Preservation Meeting"));
        events.Add(new Event(3, new DateTime(2018, 1, 5), new DateTime(2018, 1, 6), "Theatre Royal", 120, 5.00, "Traditional Irish Dancing"));

        // Fill Hospitality list
        hosps.Add(new Hospitality(4, new DateTime(2017, 12, 21), new DateTime(2017, 12, 22), "Stadium of Light", 100, 25.00, "SAFC Charity Auction", false));
        hosps.Add(new Hospitality(5, new DateTime(2017, 11, 19), new DateTime(2017, 11, 20), "Empire Cinema", 200, 7.50, "School Cinema Day", true));

        // Fill Conference list
        confs.Add(new Conference(6, new DateTime(2017, 11, 15), new DateTime(2017, 11, 16), "Marriot Hotel", 400, 5.00, "NHS Christmas Night Out", false, false));
        confs.Add(new Conference(7, new DateTime(2017, 12, 31), new DateTime(2018, 1, 1), "Hilton Hotel", 500, 10.00, "Police New Years Eve Meeting", true, true));
        confs.Add(new Conference(8, new DateTime(2018, 2, 4), new DateTime(2018, 2, 5), "Stadiun of Light", 1000, 7.50, "Duke of Edinburgh Award", true, true));
        confs.Add(new Conference(9, new DateTime(2018, 3, 5), new DateTime(2018, 3, 6), "St.Peters Campus", 500, 0, "Results Day", false, true));

    }

事件是款待和會議,款待和會議以及事件的兩個子類的超類。

當前使用此方法在我的列表框中一次顯示一個列表:

private void btnSeenormal_Click(object sender, EventArgs e)
    {
        // Clear list box before entering information
        liDisplay.DataSource = null;

        // Adding list items of type Event to list box
        liDisplay.DataSource = events;
    }

如何同時顯示所有3個列表,或者如何將3個列表合並為一個列表?

提前致謝。

對於您的示例,使用以下兩個類對其進行了一些簡化:

public class Students
{
    public string Name { get; set; }
    public string Year { get; set; }
}
public class Teachers
{
    public string Name { get; set; }
    public int TeacherId { get; set;}
}

然后,您無需弄清要顯示的內容,但是對於簡單的“ 僅顯示一些值”,您可以選擇類屬性,然后將它們首先添加到新列表中,或者直接添加到控件中,如下所示:

        List<Students> students = new List<Students>()
        {
            new Students() {Name = "Ray", Year = "2017"},
            new Students() { Name = "Carla", Year = "2019" }
        };

        List<Teachers> teachers = new List<Teachers>()
        {
            new Teachers() {Name = "Mr. Smith", TeacherId = 151},
            new Teachers() {Name = "Mrs. Barbara", TeacherId = 901}
        };

        listBox1.Items.AddRange(teachers.Select(x => x.Name).ToArray());
        listBox1.Items.AddRange(students.Select(s => s.Name).ToArray());

但是,這並不是最高效的方法,但是對於小型數據集來說效果很好。

考慮多態性時的情況。 鑒於

  1. 招待IS-A活動
  2. 會議IS-A活動

您可以通過以下方式將所有3個列表合並為一個事件列表:

events.AddRange(hosps);
events.AddRange(confs);

或者,如果您想將當前事件列表分開,並將它們組合在另一個變量中:

List<Event> allEvents = new List<Event>();
allEvents.AddRange(events)
allEvents.AddRange(hosps);
allEvents.AddRange(confs);

在針對allEvents調用AddRange之前,請確保已將項目添加到事件,hosps和confs變量中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM