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; }
}
}