保存我的对象 "student" 并将其加载到我的组合框中的最佳方法是什么?

What is the best way to save my Object "student" and load it into my combobox?

我创建了一个名为 Student 的对象。我的程序的想法是,创建多个学生并将它们保存在 .txt 文件中 加载它们。 (我希望它们在我打开程序时显示在 CombBbox 中。)

我正在使用按钮 btnAddStudent 保存新学生并将其显示在 ComboBox 中,同时使用 BindingList<Student> StudentCollection.

目前我正在努力寻找 Saving/Loading ComboBox 数据的方法,因为我只知道如何将简单文本保存到 .txt 文件。

这是我正在使用的代码。

public partial class Form1 : Form
{
    string FilePath = (@"C:\Users\hholke\Desktop\sickprogramming\Projekt3\StudentList");
    public Student ID01;
    BindingList<Student> StudentCollection = new BindingList<Student>();

    public Form1()
    {
        InitializeComponent();
    }

    #region Textbox
    private void txtFirstName_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar) && !char.IsWhiteSpace(e.KeyChar))
        {
            e.Handled = true;
            MessageBox.Show("Nur Buchstaben erlaubt");
        }
    }
    private void txtLastName_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (!char.IsLetter(e.KeyChar) && !char.IsControl(e.KeyChar) && !char.IsWhiteSpace(e.KeyChar))
        {
            e.Handled = true;
            MessageBox.Show("Nur Buchstaben erlaubt");
        }
    }
    #endregion

    #region Buttons
    private void btnLaden_Click(object sender, EventArgs e)
    {
        Student StudentLoad = (Student)cbxStudentIDs.SelectedItem;

        txtStudentID.Text = StudentLoad.ID;
        txtFirstName.Text = StudentLoad.FirstName;
        txtLastName.Text = StudentLoad.LastName;
        txtSchoolClass.Text = StudentLoad.Schoolclass;
        nudAge.Value = StudentLoad.Age;
        nudHeight.Value = StudentLoad.Height;
        cbxGender.Text = StudentLoad.Gender;
    }

    private void btnAddStudent_Click(object sender, EventArgs e)
    {
        Student StudentSave = new Student
        {
            ID = txtStudentID.Text,
            FirstName = txtFirstName.Text,
            LastName = txtLastName.Text,
            Age = nudAge.Value,
            Height = nudHeight.Value,
            Schoolclass = txtSchoolClass.Text,
            Gender = cbxGender.Text,
        };
        cbxStudentIDs.DataSource = StudentCollection;
        cbxStudentIDs.DisplayMember = "ID";
        StudentCollection.Add(StudentSave);
    }
    #endregion
    public class Student
    {
        //Eigenschaften
        public string ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public decimal Age { get; set; }
        public decimal Height { get; set; }
        public string Schoolclass { get; set; }
        public string Gender { get; set; }
    }
}

要保存和加载学生列表,您可以使用人类可读的 JSON format

将对象转换为 JSON 字符串很容易。首先,我更喜欢通过 Nuget 包管理器控制台安装 Newtonsoft.Json:

Install-Package Newtonsoft.Json -Version 12.0.3

并使用它:

using Newtonsoft.Json;

然后您可以序列化整个学生列表并将其保存到文件中:

var studentCollectionString = JsonConvert.SerializeObject(StudentCollection);
File.WriteAllText(/*FILE PATH*/, studentCollectionString);

如果你想加载学生,你只需要从文件中读取所有文本并像这样反序列化它:

var studentCollectionString = File.ReadAllText(/*FILE PATH*/);
StudentCollection = JsonConvert.DeserializeObject<BindingList<Student>>(studentCollectionString);

希望对您有所帮助。