Comparing Fields of Multiple TextBox to Check for Duplicity

0

I'm developing a basic program in C # where the user will enter values through a bar code reader. The same will be saved in an Excel spreadsheet for a future report.

My problem is that there are situations where the user bipes the same code twice in different textbox.

I wanted to know if, by clicking on my button Salvar , I had an event to compare all my textboxes and check for repeated values, preventing me from saving duplicate information.

Below is my Button's code Add:

private void BtnAdicionar_Click(object sender, EventArgs e)
{                
    Microsoft.Office.Interop.Excel.Application Excel = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook sheet = Excel.Workbooks.Open("C:\Users\Core i3\Desktop\EtiqMasterBoxTornozeleira.xlsx");
    Microsoft.Office.Interop.Excel.Worksheet x = Excel.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet;

    Excel.Range userRanger = x.UsedRange;

    int countRecords = userRanger.Rows.Count;
    int add = countRecords + 1;
}
    
asked by anonymous 21.10.2016 / 20:51

2 answers

2

A simple way to do this is to use an Array

        Int64[] valores = new Int64[3];
        valores[0] = Convert.ToInt64(textBox1.Text);
        valores[1] = Convert.ToInt64(textBox2.Text);
        valores[2] = Convert.ToInt64(textBox3.Text);


        int quant_campos = valores.Length;
        var groups = valores.Distinct().ToList();

        if(quant_campos > groups.Count)
        {

            MessageBox.Show("Existe Campos Duplicados");
        }
    
22.10.2016 / 13:05
0

Some things have not been very clear, but I will try to help. You comment on iterating the textboxes, so I'm going to conclude that you do not need to confirm the information that has already been written to your worksheet.

Well:

Save each barcode (string) into an collection of strings of your choice ; but rather confirm that within this list it does not yet contain this "barcode" that will be saved through the Contains :

ICollection<string> codesCollection = new List<string>();

private void AddBarCode(string barcode)
{
    bool isDuplicatedCode = codesCollection.Contains(barcode);

    if(isDuplicatedCode)
        throw new ArgumentException("O código de barras já foi adicionado.").

    codesCollection.add(barcode);
}
    
22.10.2016 / 09:58