如何防止重复条目进入列表框

时间:2019-04-17 04:27:32

标签: c# winforms

我要创建一个从输出文件读取的程序,并通过“ AppendText”方法向该输出文件中添加更多文本,以便覆盖文本文件中的所有内容。您可以通过文本框将内容添加到列表框中,但是我想做的是防止重复输入。我嵌入了一个代码,该代码据说可以防止多个条目,但是不能正常工作。它提示我设置了“ Duplicate entry”,但仍添加了该条目。有任何解决这个问题的方法吗?请帮忙。

这是代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


namespace BIT_UNITS
{
public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private void displayButton_Click(object sender, EventArgs e)
    {
        try
        {
            //Variables
            string unitsList;

            //declare streamReader variable
            StreamReader inputFile;

            //Open file & get units list
            inputFile = File.OpenText("BITS_Units.txt");

            //Clear anything currently in the listbox
            unitsListBox.Items.Clear();

            //Read the file's Contents
            while (!inputFile.EndOfStream)
            {
                //Get Units List
                unitsList = inputFile.ReadLine();

                //Display the units list in the listbox
                unitsListBox.Items.Add(unitsList);
            }
            //close the file
            inputFile.Close();

        }
        catch 
        {
            MessageBox.Show("Error");
        }
    }

    private void addUnitButton_Click(object sender, EventArgs e)
    {
        try
        {
            //Declare streamwriter variable
            StreamWriter outputFile;


            //Open file and get a streamwriter object
            outputFile = File.AppendText("BITS_Units.txt");

            //Record inputs to the file
            outputFile.WriteLine(addUnitsTextBox.Text);

            //Close the file 
            outputFile.Close();

            //Determine wether textbox is filled
            if (addUnitsTextBox.Text== Text)
            {
            //Display message
            MessageBox.Show("Unit was successfully added.");
            }

            //Determine wether textbox is filled                
            if (addUnitsTextBox.Text == "")
            {
                MessageBox.Show("Please enter a unit name to add to the list.");

            }

            if (unitsListBox.Items.Contains(addUnitsTextBox.Text))
            {

                MessageBox.Show("This unit already exists");
            }

            else 
            {
                unitsListBox.Items.Add(addUnitsTextBox.Text);
                addUnitsTextBox.Text = "";

            }

          }
        catch (Exception)
        {
            MessageBox.Show("error");
        }
    }

    private void clearButton_Click(object sender, EventArgs e)
    {
        try
        {
            //Clear data
            addUnitsTextBox.Text = "";
            unitsListBox.Items.Clear();
        }
        catch (Exception)
        {
            MessageBox.Show("Error");
        }
    }

    private void exitButton_Click(object sender, EventArgs e)
    {
        //Close the form
        this.Close();
    }
}
}

1 个答案:

答案 0 :(得分:2)

在将项目添加到列表框之前,请检查列表框中是否不存在。

     if (!unitsListBox.Items.Contains(unitsList) )
     {
          unitsListBox.Items.Add(unitsList);
     }