0

I created an Object that is called Student. The idea of my Program is, to create multiple Students and save them in a .txt File and load them. (I'd like them to be displayed in the CombBbox, when I open the program.)

I'm using the Button btnAddStudent to Save new Students and display them in a ComboBox, whilst using BindingList<Student> StudentCollection.

Currently I'm struggling with finding a way for Saving/Loading the ComboBox Data, cause I only know how to save simple Text to .txt Files.

This is the Code I'm using.

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; }
    }
}
  • 1
    This should be helpful [link](https://stackoverflow.com/questions/6115721/how-to-save-restore-serializable-object-to-from-file) – DaszuOne Feb 28 '20 at 10:00
  • You'd better to save it to .xml or .json file than .txt. Check the link [link](https://support.microsoft.com/en-us/help/815813/how-to-serialize-an-object-to-xml-by-using-visual-c) – Jongkwan Park Feb 28 '20 at 10:36

1 Answers1

1

To save and load a list of students you could use the JSON format which is human readable.

Converting an object to JSON string is easy. First I prefer to install Newtonsoft.Json through the Nuget Package Manager Console:

Install-Package Newtonsoft.Json -Version 12.0.3

And use it:

using Newtonsoft.Json;

Then you can serializer the entire students list and save it to a file:

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

If you want to load the students, you just need to read all text from the file and deserialize it like this:

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

I hope this helped.

Sven St
  • 26
  • 2