【问题标题】:Display mutliple lists in a list box在列表框中显示多个列表
【发布时间】:2017-11-01 04:21:34
【问题描述】:

我正在使用 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));

    }

Event 是 Hospitality and Conference、Hospitality and Conference 以及 Event 的两个子类的超类。

目前使用这种方法在我的列表框中一次显示一个列表:

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 个列表合并为一个?

提前致谢。

【问题讨论】:

  • 你应该做events.Add而不是hosps.Addconfs.Add
  • 我看不出这样做的原因,因为那时我只有 1 个列表,我的任务是按类型将信息显示回用户。因此,表单上有 4 个按钮,用户可以选择显示正常活动、招待活动和会议活动以及所有活动。
  • 您仍然可以维护 3 个列表,并将招待和会议对象添加到事件列表及其各自的列表中。
  • 您能否举个例子说明如何做到这一点,我对编码还很陌生,所以不知道该怎么做。
  • 你能分享一下事件、款待和会议的类定义吗?

标签: c# list class listbox multiple-inheritance


【解决方案1】:

考虑您的情况并考虑Polymophism。鉴于

  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 之前已将项目添加到 events、hosps 和 confs 变量。

【讨论】:

    【解决方案2】:

    使用以下两个类对您的示例进行了一些简化:

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

    然而,这并不是最高效的方法,但对于小型数据集来说效果很好。

    【讨论】:

    • 感谢您的帮助,终于完成了程序,我只需要知道如何使用该数组函数添加多个列表。添加了 3 个列表,它运行良好。再次感谢大家。
    • @OliverAllen 太好了!很高兴我们能提供帮助。请记住为对您有帮助的答案投票并接受对未来搜索者有帮助的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多