我想實現的是如下:每當我添加一個student
它應該檢查ListBox
如果裏面有重複項。如果是這種情況,它應該顯示一個MessageBox
,並防止將項目添加到ListBox
。檢查重複列表框中的重複項
這是我的時刻代碼:
private void buttonAdd_Click(object sender, EventArgs e)
{
Student student = GetStudent();
Repository.AddStudent(student);
if (listBoxStudents.Items.Contains(student))
{
MessageBox.Show("This student already exists!");
}
else
{
listBoxStudents.Items.Add(student);
ClearandFocus();
}
}
我不知道爲什麼我的代碼不能正常工作,輸入來自幾個方面TextBoxes
在被添加到一個List<Students>
和ListBox
一種形式。
提示:'Contains()'方法以什麼類型作爲參數? –
你有沒有可以使用的ID? –
'listBoxStudents.Items.Select(s => s.StudentId).Contains(student)' –