Mozz Jonas
Mozz Jonas

Reputation: 9

How to prevent duplicates entries into a listbox

I am to create a program that reads from an outputfile AND also add FURTHER text to that output file trough "AppendText" method so that nothing in the text file is overriden. You can add things to the listbox via a textbox, but what I am trying to do is to prevent duplicate entries. I have implmeneted a code that does supposedly prevents multiple entries, but it doesn't work properly. It gives a message that I set "Duplicate entry" but it still adds the entry. ANY WAY TO FIX THIS? Please Help THANKS.

This is the code

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

Upvotes: 1

Views: 677

Answers (1)

Amir Molaei
Amir Molaei

Reputation: 3820

Before adding item to list box, check if doesn't exist in the list box.

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

Upvotes: 2

Related Questions