如何在Visual Studio中使用WinForms自定义类的值填充ListBox?

lrpiutwd  于 7个月前  发布在  其他
关注(0)|答案(2)|浏览(63)

在我的WinForms项目中,我有一个ListBox控件以及多个标签。我还有一个自定义的Person类,其中的值从文本文件CSV中分配。这个Person类具有Name, Gender, Weight,等属性。
我希望发生的是将Person类中的所有Name属性添加到ListBox控件中。然后,当用户通过单击ListBox中的他/她来选择此Person时,标签得到更新的人相应的值。我希望这是有意义的。我已经包括了一个图像的形式,以帮助您更好地了解什么我已经尝试了下面的代码,但是列表框没有更新任何值。

public void Get_Roster()
{
    string line;
    List<Person> people = new List<Person>();

    System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\Austin316\Desktop\yourFile.txt");

    while ((line = file.ReadLine()) != null)
    {
        string[] words = line.Split(',');

        Person p = new Person(words[0], words[1], words[2], words[3], words[4], words[5], words[6], words[7], words[8], words[9], words[10], words[11]);

        foreach (Person b in people)
        {
            lstRoster.Items.Add(p.Name).ToString();
        }
    }

    file.Close();
}

字符串

q35jwt9p

q35jwt9p1#

你有问题在你的代码填充listbpx控件请参阅此代码可能会帮助你:

public void Get_Roster()
{
    string line;
    List<Person> people = new List<Person>();

    System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Users\Austin316\Desktop\yourFile.txt");

    while ((line = file.ReadLine()) != null)
    {
        string[] words = line.Split(',');

        Person p = new Person(words[0], words[1], words[2], words[3], words[4], words[5], words[6], words[7], words[8], words[9], words[10], words[11]);

        // Add the person to the list
        people.Add(p);
        // Add the person's name to the ListBox
        lstRoster.Items.Add(p.Name);
    }

    file.Close();
}

字符串
现在,每次从文件中读取一行并创建一个Person对象时,都将该人添加到人员列表和ListBox中。这应该会正确地使用文件中的人员名称填充ListBox。
要处理选择事件并更新标签,可以使用ListBox的SelectedIndexChanged事件:

private void lstRoster_SelectedIndexChanged(object sender, EventArgs e)
{
    // Get the selected person from the people list
    Person selectedPerson = people[lstRoster.SelectedIndex];

    // Update labels with the selected person's information
    lblName.Text = selectedPerson.Name;
    lblGender.Text = selectedPerson.Gender;
    // Add more label updates for other properties as needed
}

public YourForm()
{
    InitializeComponent();
    lstRoster.SelectedIndexChanged += lstRoster_SelectedIndexChanged;
}


将YourForm替换为表单类的实际名称。

qoefvg9y

qoefvg9y2#

创建一个列表并将每个人添加到列表中,然后将列表设置为ListBox的DataSource属性,而不是在循环中添加每个项。
若要显示Name属性,请添加如下所示的.ToString,它将成为ListBox的DisplayMember。

class Person
{
    public string Name { get; set; }
    public string Gender { get; set; }
    public int Weight { get; set; }
    public override string ToString() => Name;
}

字符串
模拟加载数据和将name属性绑定到标签的数据。根据需要继续添加其他标签。

List<Person> list = new List<Person>
{
    new Person() { Name = "Anne", Gender = "Female", Weight = 99},
    new Person() { Name = "Jon", Gender = "Male", Weight = 150}
};

lstRoster.DataSource = list;
NameLabel.DataBindings.Add("Text", list, "Name");


如果您需要获取所选项目,请使用以下命令

if (lstRoster.SelectedItem is not null)
{
    var person = (Person) lstRoster.SelectedItem;
    MessageBox.Show($"{person.Name}, {person.Gender}");
}

相关问题