listClass - main function: create a list of Person classpublic class listClass{ public List<Person> People{ get; set; } = new List<Person>();} 在我的主表单中,我有两个按钮AddPerson和AddDate我想要实现的是允许(通过从dateTimePicker中选择日期)将日期数添加到人员列表中的一个对象,即一个人可以有2-3个日期。 我是什么尝试过: 我已经正确添加了人物。In my main form, I have two button AddPerson and AddDate what I'm trying to achieve is to allow (by choosing date from dateTimePicker) add number of dates to one object inside the Person list, i.e one person can have 2-3 dates.What I have tried:I have added person correctly.private void SaveDriver(object sender, EventArgs e) { Person p = new Person(); listClass list = new listClass(); p.Name = txtFirstName.Text; p.Surname = txtSurname.Text; p.Age = int.Parse(txtAge.Text); list.People.Add(p); } 这是正常的,它将此人添加到与 Person class 相关的人员列表中 我不知道该怎么做,我想如何添加日期数,即一个人可以有3个日期。 我创建了一个按钮AddDate:This works correctly, it adds the person into a person list which is related to the Person classWhat I don't know how to do, is how I suppose to add number of dates into it, i.e one person can have 3 dates.I have created a button AddDate:private void btnAddDate_Click_1(object sender, EventArgs e) { People p = new People(); Date d = new Date(); d.date = dtpDate.Value; //date time picker p.dates.Add(d); //trying to add the dates into the People list } 我知道最后一段代码不正确,因为每当我点击添加日期按钮时,它都不会将日期从DateTimePicker添加到List< ;日期>在Person Class中的日期,所以我想如何允许用户为个人对象添加日期数? 提前感谢。I know the last bit of code is incorrect, because whenever I click the add date button it does not add the date from DateTimePicker to the List<date> dates in Person Class so how I suppose to allow the user to add number of dates to a person object?Thanks in advance.推荐答案嘿,在下面的表单类中创建people对象Hey, create people object outside but within the form class like belowPublic FormPeople: Window{ People people = new People(); IList<People> peoples = new List<People>();Public FormPeople() {people.dates = new List<Date>(); }private void btnAddDate_Click_1(object sender, EventArgs e) { Date d = new Date(); d.date = dtpDate.Value; //date time picker people.dates.Add(d); //trying to add the dates into the People list }}private void AddPeople(){ peoples.add(people);}我假设你使用Visual Studio IDE,在项目中你可以声明类。你已经定义了这些类并不重要。I assumed you use Visual Studio IDEand within the project you can declare classes. It doesn't matter you have already defined those classes. 这篇关于C#将值添加到列表中的列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 10:13
查看更多