Disabling TextBox not filled [closed]

1

I am developing software in C # to count the number of electronic anklets that go inside a box. This box has space to store 40 anklets. In the case of the Software I put 40 txtbox where each one represents the serial number of each ankle. As it occurs the operator sometimes repeat the same ancestor in different txtbox, with the help of an acquaintance I developed this method down which indicates if there are repeated values within those txtbox which in case were placed in an Array [39]. So far so good, what happens is that not always 40 txtbox are filled because sometimes the boxes are incomplete, the problem is that the method indicates error of duplicity because as there are fields that are empty, maybe it understands as if they were repeating values. I would like to know if, I have how to disable the textbox that are not filled. This condition of the code below was put in the btsalvararquivo. I put the code where I get the information from the textbox in Array and it does a check.

Code:

    private void BtnSair_Click(object sender, EventArgs e)
    {
        System.Windows.Forms.Application.Exit();
    }

    //Evento onde é feita a verificação de duplicidade da etiqueta.
    private void BtnAdicionar_Click(object sender, EventArgs e)
    {
        Int64[] valores = new Int64[40];
        //RATE 01-10
        valores[0] = Convert.ToInt64(textBox1.Text);
        valores[1] = Convert.ToInt64(textBox3.Text);
        valores[2] = Convert.ToInt64(textBox4.Text);
        valores[3] = Convert.ToInt64(textBox5.Text);
        valores[4] = Convert.ToInt64(textBox6.Text);
        valores[5] = Convert.ToInt64(textBox7.Text);
        valores[6] = Convert.ToInt64(textBox8.Text);
        valores[7] = Convert.ToInt64(textBox9.Text);
        valores[8] = Convert.ToInt64(textBox10.Text);
        valores[9] = Convert.ToInt64(textBox11.Text);

        //RATE 11-20
        valores[10] = Convert.ToInt64(textBox12.Text);
        valores[11] = Convert.ToInt64(textBox13.Text);
        valores[12] = Convert.ToInt64(textBox14.Text);
        valores[13] = Convert.ToInt64(textBox15.Text);
        valores[14] = Convert.ToInt64(textBox16.Text);
        valores[15] = Convert.ToInt64(textBox17.Text);
        valores[16] = Convert.ToInt64(textBox18.Text);
        valores[17] = Convert.ToInt64(textBox19.Text);
        valores[18] = Convert.ToInt64(textBox20.Text);
        valores[19] = Convert.ToInt64(textBox21.Text);

        //RATE 21-30
        valores[20] = Convert.ToInt64(textBox22.Text);
        valores[21] = Convert.ToInt64(textBox23.Text);
        valores[22] = Convert.ToInt64(textBox24.Text);
        valores[23] = Convert.ToInt64(textBox25.Text);
        valores[24] = Convert.ToInt64(textBox26.Text);
        valores[25] = Convert.ToInt64(textBox27.Text);
        valores[26] = Convert.ToInt64(textBox28.Text);
        valores[27] = Convert.ToInt64(textBox29.Text);
        valores[28] = Convert.ToInt64(textBox30.Text);
        valores[29] = Convert.ToInt64(textBox31.Text);

        //RATE 31-40
        valores[30] = Convert.ToInt64(textBox32.Text);
        valores[31] = Convert.ToInt64(textBox33.Text);
        valores[32] = Convert.ToInt64(textBox34.Text);
        valores[33] = Convert.ToInt64(textBox35.Text);
        valores[34] = Convert.ToInt64(textBox36.Text);
        valores[35] = Convert.ToInt64(textBox37.Text);
        valores[36] = Convert.ToInt64(textBox38.Text);
        valores[37] = Convert.ToInt64(textBox39.Text);
        valores[38] = Convert.ToInt64(textBox40.Text);
        valores[39] = Convert.ToInt64(textBox41.Text);

        //Verificando a Duplicidade 
        int quant_campos = valores.Length;
        var groups = valores.Distinct().ToList();


        if (quant_campos > groups.Count)
        {
            MessageBox.Show("EXISTEM VALORES DUPLICADOS", "ATENÇÃO!!!", MessageBoxButtons.OK, MessageBoxIcon.Stop);

        }

        return;
    
asked by anonymous 26.10.2016 / 22:32

1 answer

1

Your code has several serious flaws that can cause error at runtime,

  • If the user types a non-number character you will get a conversion error. Convert.ToInt64(textBox1.Text);
  • Very strange you specify that duplication can occur due to some field not being filled, if it becomes NULL you will also get the error above.
  • You could specify to the user which field is duplicitous.
  • I've put the code below with a start on how you can validate your code, just follow the logic and continue with the rest of the fields, but you'd better rethink how to do that.

    using System;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            //Evento onde é feita a verificação de duplicidade da etiqueta.
            private void BtnAdicionar_Click(object sender, EventArgs e)
            {
                Int64[] valores = new Int64[40];
                //RATE 01-10
                if (!string.IsNullOrEmpty(textBox1.Text) && checarString(textBox1.Text))
                    valores[0] = Convert.ToInt64(textBox1.Text);
    
                if (!string.IsNullOrEmpty(textBox3.Text) && checarString(textBox3.Text))
                {   
                    // usar o linq para verificar se existem textBox iguais
                    if (!valores.Any(v => v.Equals(Convert.ToInt64(textBox3.Text))))
                    {
                        valores[1] = Convert.ToInt64(textBox3.Text);
                    }
                    else
                    {
                        messageBoxButtons();
                    }
                }
    
                valores[2] = Convert.ToInt64(textBox4.Text);
                valores[3] = Convert.ToInt64(textBox5.Text);
                valores[4] = Convert.ToInt64(textBox6.Text);
                valores[5] = Convert.ToInt64(textBox7.Text);
                valores[6] = Convert.ToInt64(textBox8.Text);
                valores[7] = Convert.ToInt64(textBox9.Text);
                valores[8] = Convert.ToInt64(textBox10.Text);
                valores[9] = Convert.ToInt64(textBox11.Text);
    
                //RATE 11-20
                valores[10] = Convert.ToInt64(textBox12.Text);
                valores[11] = Convert.ToInt64(textBox13.Text);
                valores[12] = Convert.ToInt64(textBox14.Text);
                valores[13] = Convert.ToInt64(textBox15.Text);
                valores[14] = Convert.ToInt64(textBox16.Text);
                valores[15] = Convert.ToInt64(textBox17.Text);
                valores[16] = Convert.ToInt64(textBox18.Text);
                valores[17] = Convert.ToInt64(textBox19.Text);
                valores[18] = Convert.ToInt64(textBox20.Text);
                valores[19] = Convert.ToInt64(textBox21.Text);
    
                //RATE 21-30
                valores[20] = Convert.ToInt64(textBox22.Text);
                valores[21] = Convert.ToInt64(textBox23.Text);
                valores[22] = Convert.ToInt64(textBox24.Text);
                valores[23] = Convert.ToInt64(textBox25.Text);
                valores[24] = Convert.ToInt64(textBox26.Text);
                valores[25] = Convert.ToInt64(textBox27.Text);
                valores[26] = Convert.ToInt64(textBox28.Text);
                valores[27] = Convert.ToInt64(textBox29.Text);
                valores[28] = Convert.ToInt64(textBox30.Text);
                valores[29] = Convert.ToInt64(textBox31.Text);
    
                //RATE 31-40
                valores[30] = Convert.ToInt64(textBox32.Text);
                valores[31] = Convert.ToInt64(textBox33.Text);
                valores[32] = Convert.ToInt64(textBox34.Text);
                valores[33] = Convert.ToInt64(textBox35.Text);
                valores[34] = Convert.ToInt64(textBox36.Text);
                valores[35] = Convert.ToInt64(textBox37.Text);
                valores[36] = Convert.ToInt64(textBox38.Text);
                valores[37] = Convert.ToInt64(textBox39.Text);
                valores[38] = Convert.ToInt64(textBox40.Text);
                valores[39] = Convert.ToInt64(textBox41.Text);
    
                return;
            }
            // verifica se realmente é só números
            public bool checarString(string str)
            {
                char[] c = str.ToCharArray();
                char le = ' ';
                for (int cont = 0; cont < c.Length; cont++)
                {
                    le = c[cont];
                    if (!char.IsDigit(le))
                        return false;
                }
                return true;
            }
            // retorna a mensagem ... pense em passar como parâmetros os campos duplicados ....
            public void messageBoxButtons()
            {
                MessageBox.Show("EXISTEM VALORES DUPLICADOS", "ATENÇÃO!!!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }
    }
    
        
    28.10.2016 / 02:30