Disabling Unfilled Textbox

Asked

Viewed 178 times

1

I’m developing software in c# to count the number of electronic anklets that go inside a box. This box has room to store 40 anklets. In the case of Software I put 40 txtbox where each one represents the serial number of each anklet. As it happens the operator sometimes repeat the same anklet in different txtbox, with the help of a known I developed this method below which indicates if there are repeated values within these txtbox that in the case were placed in an Array[39]. So far so good, what happens is that not always the 40 txtbox are filled because sometimes the boxes are incomplete, the problem is that the method indicates duplicity error because as there are fields that are empty, maybe it understands as if they were repeating values. I wonder if, have how I disable the textbox that are not filled. This condition of the code below was put in btsalvarfile. I put the code where I capture the textbox information in the 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;
  • Luiz, your question is not clear enough that you can understand and write an answer, please edit it in a way that is better understood. For examples how are you doing to read the txtbox ? and how are they loaded? where the error is occurring ?

  • int quant_groups = values. Where(item => item != null). length; or var quant_groups = values.Where(item => !string.Isnullorempty(item)). length length;

  • It is difficult to give an answer without knowing what you are manipulating, I believe that your verification can be simpler than this trying to do.

  • I put more code information, please make sure it’s clearer where I’m going.

1 answer

1


Your code has several serious faults that can cause error at the time of execution,

  1. If user type a character other than number you will get a conversion error. Convert.ToInt64(textBox1.Text);
  2. Very strange you specify that duplicity may occur due to some field not being filled in, if it stays as NULL you will also have the above error.
  3. You could specify to the user which field is in duplicity.

I built 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 still rethink a better way to do this.

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);
        }
    }
}
  • Actually the method that you reported worked, only that it didn’t take was the part of checking if the field was being filled with Letters, but I solved it using the doing a method and pulling straight from the Textbox keypress. Only what is missing is to develop the method so that it shows the user which Textbox are repeating, or at least mark with Color, which is what I did. It didn’t work very well but I added a "Textbox1.Backcolor = Color.Yellow . It didn’t work because it just marks the second repeated box.

  • To get an idea of which is the first you would have to locate the arrey index with the duplicate value, then you would know which was the textbox that inserted the value in it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.