在列表框中显示多个列表
Display mutliple lists in a list box
我正在使用 c# Visual Basic 2017,我正在尝试创建一个列表框,它将同时显示 3 个不同的列表,我想知道这是否可行。列表框目前可以通过使用“.datasource =”函数单独显示每个列表。每个列表都是 class 类型,这些 class 都是不同的,但是有一个 superclass 和 2 个 subclasses.
这是我在表格中命名和填写列表的方式:
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 和 Conference 的超class,Hospitality 和 Conference 都是 Event 的子class。
目前正在使用此方法在我的列表框中一次显示一个列表:
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 个列表合并为一个?
提前致谢。
使用以下两个 class 对您的示例进行了一些简化:
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;}
}
然后你没有说明你想显示什么,但是为了简单 只显示一些值 你可以 select class 属性和首先将它们添加到新列表中,或者像这样直接添加到控件中:
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());
然而,这不是执行此操作的最高效方法,但适用于小型数据集。
请考虑您的情况 Polymophism。鉴于
- 款待IS-A事件
- 会议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 变量。
我正在使用 c# Visual Basic 2017,我正在尝试创建一个列表框,它将同时显示 3 个不同的列表,我想知道这是否可行。列表框目前可以通过使用“.datasource =”函数单独显示每个列表。每个列表都是 class 类型,这些 class 都是不同的,但是有一个 superclass 和 2 个 subclasses.
这是我在表格中命名和填写列表的方式:
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 和 Conference 的超class,Hospitality 和 Conference 都是 Event 的子class。
目前正在使用此方法在我的列表框中一次显示一个列表:
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 个列表合并为一个?
提前致谢。
使用以下两个 class 对您的示例进行了一些简化:
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;}
}
然后你没有说明你想显示什么,但是为了简单 只显示一些值 你可以 select class 属性和首先将它们添加到新列表中,或者像这样直接添加到控件中:
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());
然而,这不是执行此操作的最高效方法,但适用于小型数据集。
请考虑您的情况 Polymophism。鉴于
- 款待IS-A事件
- 会议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 变量。