C# 试图返回winForm列表框中的人名

C# 试图返回winForm列表框中的人名,c#,C#,我本质上是在列表中添加内容;使用for..循环在列表框中迭代这些名称 然而,一旦我输入姓名/年龄,它就不会显示任何姓名;单击〖增加人员〗按钮,显示人员;什么也看不出来 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using Syst

我本质上是在列表中添加内容;使用for..循环在列表框中迭代这些名称

然而,一旦我输入姓名/年龄,它就不会显示任何姓名;单击〖增加人员〗按钮,显示人员;什么也看不出来

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI_TEST_2
{
    public partial class Form1 : Form
    {
        List<Person_List> people = new List<Person_List>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void addPerson_Click(object sender, EventArgs e)
        {
            string name = addName.Text;

            int age = Convert.ToInt32(addAge.Text);

            for (int i = 0; i < people.Count(); i++)
            {
                people[i].addPersonToList(name, age);
            }
        }

        private void showPeople_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < people.Count(); i++)
            {
                string name = people[i].showPeople();

                peopleListBox.Items.Add("Name: " +  name);
            }
        }

        private void peopleListBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GUI_TEST_2
{
    class Person_List
    {
        public List<Person> listOfPeople = new List<Person>();

        public Person_List()
        {
        }

        public void addPersonToList(string name, int age)
        {
            listOfPeople.Add(new Person(name, age));
        }

        public string showPeople()
        {
            string name = "";

            for (int i = 0; i < listOfPeople.Count(); i++)
            {
                name = listOfPeople[i].Name;
            }

            return name;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
名称空间GUI_测试_2
{
公共部分类Form1:Form
{
列表人员=新列表();
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
}
私有void textBox2_TextChanged(对象发送方,事件参数e)
{
}
私有void addPerson\u单击(对象发送者,事件参数e)
{
字符串名称=addName.Text;
int age=Convert.ToInt32(addAge.Text);
for(int i=0;i
将ppl添加到listOfPeople列表后,必须将其添加到listbox。将它们添加到listOfPeople并不意味着它们会自动添加到listbox。据我所知,没有双向绑定

我将如何实施

showPeople(){
    listBox1.Items.Clear();
    foreach (var ppl in listOfPeople)
    {
        listBox1.Items.Add(ppl)
    }
}

在addPerson\u Click事件中,您循环查看您的列表人员,但此列表不包含任何内容,则您永远不会调用addPersonToList

试着做一些事情,比如:

Person_List people = new Person_List();

private void addPerson_Click(object sender, EventArgs e)
{
    string name = addName.Text;

    int age = Convert.ToInt32(addAge.Text);

    people.addPersonToList(name, age);
}

您似乎没有将任何
Person\u List
对象添加到
people
对象。因为我看到的
people
是空的,并且您从未添加任何元素(至少在您发布的代码中)。您有一个列表。列表的初始列表是空的,这就是为什么addPerson\u Click中没有添加任何内容,我想我理解;因为我从来没有将对象添加到Person_列表中的人物列表中;它没有索引来遍历任何内容。