我正在使用c#Visual Basic 2017,我正在尝试创建一个列表框,它将同时显示3个不同的列表,并想知道这是否可行。列表框当前可以通过使用" .datasource ="显示它自己的每个列表。功能。每个列表都是类型类,这些类都是不同的,但是有一个超类和2个子类。
这就是我在表单中命名和填写列表的方式:
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个列表合并为一个?
提前致谢。
答案 0 :(得分:0)
使用以下两个类简化它的示例:
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 :(得分:0)
考虑到Polymophism时的情况。鉴于此
您可以通过执行
将所有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变量添加了项目。