繁体   English   中英

在添加相同的新条目之前,如何检查我的列表框是否已包含相同的条目?

[英]How do I check if my list box already contains the same entry before adding the same new one?

我试图制作一个简单的 C# Windows 窗体应用程序 .NET Framework,其中我有一个类(字符串名称、字符串姓氏、字符串课堂、字符串日期日期),其中包含三个文本框条目和一个日期时间选择器条目。 所有这些条目都保存到一个列表和一个列表框中。 如果我试图放入列表/列表框中的条目已经在列表框中,我需要添加到我的 if 语句中以检查每个语句。 如果条目重复,它会给出一个消息框错误,如果它不重复,它将使用 if 语句的延续来添加它。

这是我对重复检查的尝试:

private void btnAdding_Click(object sender, EventArgs e)
{
    if (txtName.Text != "" && txtLastName.Text != "" && txtClassroom.Text != "" )
    {
        if (ListBox.Items.Cast<string>().Contains(ListBox.Items.ToString()))
        {
            MessageBox.Show("This entry already exists.", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else { 
        List.Add(new Student(txtName.Text, txtLastName.Text, txtClassroom.Text, dateDay.Value.ToString("dd.MM.yyyy")));
        RefreshCheck();
        }
    }
    else
    {
        MessageBox.Show("You didnt fill out all the text boxes.", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

public void RefreshCheck()
        {
            ListBox.Items.Clear();
            for (int i = 0; i < seznam.Count(); i++)
            {
                ListBox.Items.Add(List[i].StudentAdd());
                this.txtName.Clear();
                this.txtLastName.Clear();
                this.txtClassroom.Clear();
                this.dateDay.Value = DateTime.Today;
                this.tabStudent.SelectedTab = this.tabCheck;
            }
        }

这是我没有重复检查的原始代码(只是 if 语句):

private void btnAdding_Click(object sender, EventArgs e)
{
    if (txtName.Text != "" && txtLastName.Text != "" && txtClassroom.Text != "" )
    {
        List.Add(new Student(txtName.Text, txtLastName.Text, txtClassroom.Text, dateDay.Value.ToString("dd.MM.yyyy")));
        RefreshCheck();
    }
    else
    {
        MessageBox.Show("You didnt fill out all the text boxes.", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

public void RefreshCheck()
        {
            ListBox.Items.Clear();
            for (int i = 0; i < List.Count(); i++)
            {
                ListBox.Items.Add(List[i].StudentAdd());
                this.txtName.Clear();
                this.txtLastName.Clear();
                this.txtClassroom.Clear();
                this.dateDay.Value = DateTime.Today;
                this.tabStudent.SelectedTab = this.tabCheck;
            }
        }

我不知道如何设置 for each 循环来检查列表框/列表条目。 如果你知道怎么做,请帮忙。

这是我如何添加到列表框中的代码:

List.Add(new Student(txtName.Text, txtLastName.Text, txtClassroom.Text, dateDay.Value.ToString("dd.MM.yyyy")));

以下是该类返回添加到列表框中的值的方式:

public string StudentAdd()
    {
        return $"{this.Name} {this.LastName} | {this.Clasroom} | {this.DateOfLeave}";
    }

编辑:更改了 OsveziPregledDijaka() -> RefreshCheck,向代码添加了 RefreshCheck 函数。

您可以使用 LINQ 表达式来查看该项目是否存在。

if(ListBox.Items.Any(i=>i.Name == txtName.Text && i.LastName == txtLastName.Text && i.ClassRoom == txtClassRoom.Text)
{
   //Show MessageBox
}

您需要搜索列表框显示的字符串。 但是上面的代码使用了 ListBox.Items.ToString(),这只是 ListBox 类名,而不是 ListBox 显示的各个字符串。

因此,假设您的列表框使用名字、姓氏和教室显示有关学生的信息,您需要构建一个类似于项目集合显示的字符串

 string searchFor = $"{txtName.Text} {txtLastName.Text} {txtClassRoom.Text}";

并使用该字符串搜索项目集合(无需调用 Cast())

 if (boxListBox.Items.Contains(searchFor))
    ....
 else
    ....

这里重写了完整的事件点击处理程序

private void btnAdding_Click(object sender, EventArgs e)
{
    if (txtName.Text != "" && txtLastName.Text != "" && txtClassroom.Text != "" )
    {
        string searchFor = $"{txtName.Text} {txtLastName.Text} {txtClassRoom.Text}";
        if (boxListBox.Items.Contains(searchFor))
        {
            MessageBox.Show("This entry already exists.", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else 
        { 
            List.Add(new Student(txtName.Text, txtLastName.Text, txtClassroom.Text, dateDay.Value.ToString("dd.MM.yyyy")));
            RefreshCheck();
        }
    }
    else
    {
        MessageBox.Show("You didnt fill out all the text boxes.", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

您可以覆盖 Student 类中的 Equals 运算符,然后使用 Contains 方法检查列表。

public override bool Equals(object obj)
{
    if (obj == null || !(obj is Student student))
    {
        return false;
    }
    return Name == student.Name && LastName == student.LastName && Classroom == student.Classroom && DateDay == student.DateDay;
}

然后检查项目:

if (!ListBox.Items.Contains(student))
{
    ListBox.Items.Add(student);
}
else
{
    MessageBox.Show("You didnt fill out all the text boxes.", "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM