An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
Hello,
To work with a list of Student in a ListBox I've wrote just enough to show adding new students and selecting the current Student in the ListBox is any. The BindingList and implementation of INotifyPropertyChanged assist with displaying data and notify of changes.
public class Student : INotifyPropertyChanged
{
private string _name;
public string Name
{
get => _name;
set
{
_name = value;
OnPropertyChanged();
}
}
public Student Highest(Student o1, Student o2) =>
string.Compare(
o1.ToString(),
o2.ToString(), StringComparison.Ordinal) > 0 ? o1 : o2;
public override string ToString() => Name;
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Form code
public partial class Form1 : Form
{
private readonly BindingList<Student> _bindingList = new BindingList<Student>();
public Form1()
{
InitializeComponent();
listBox1.DataSource = _bindingList;
}
private void AddButton_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(NameTextBox.Text))
{
_bindingList.Add(new Student() { Name = NameTextBox.Text });
}
}
private void CurrentButton_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex <= -1) return;
var student = _bindingList[listBox1.SelectedIndex];
MessageBox.Show(student.Name);
}
}
EDIT
Here is how to change the current Student done in a mock up
private void EditButton_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex <= -1) return;
_bindingList[listBox1.SelectedIndex].Name = "Karen";
listBox1.Invalidate();
}