Home > Back-end >  Determine if the textbox contains the same value as any other textbox
Determine if the textbox contains the same value as any other textbox

Time:11-28

I am using winforms using C#, I want to check if one of my textboxes has the same value as any other textbox in my textbox array. The user enters values inside the textboxes and if any duplicate value is entered, an error is displayed. When I use the textchanged event handler with the for loop to iterate through the entire array, it is checking every single text box instead of comparing the text box that only has the text changed to the other text boxes.

public partial class Form1 : Form
    {
        TextBox[,] textBoxArray = new TextBox[5, 5];
       
        public Form1()
        {
            InitializeComponent();
            textBoxArray[0, 0] = textBox1;
            textBoxArray[0, 1] = textBox2;
            textBoxArray[0, 2] = textBox3;
            textBoxArray[0, 3] = textBox4;
            textBoxArray[0, 4] = textBox5;
            textBoxArray[1, 0] = textBox6;
            textBoxArray[1, 1] = textBox7;
            textBoxArray[1, 2] = textBox8;
            textBoxArray[1, 3] = textBox9;
            textBoxArray[1, 4] = textBox10;
            textBoxArray[2, 0] = textBox11;
            textBoxArray[2, 1] = textBox12;
            textBoxArray[2, 2] = textBox13;
            textBoxArray[2, 3] = textBox14;
            textBoxArray[2, 4] = textBox15;
            textBoxArray[3, 0] = textBox16;
            textBoxArray[3, 1] = textBox17;
            textBoxArray[3, 2] = textBox18;
            textBoxArray[3, 3] = textBox19;
            textBoxArray[3, 4] = textBox20;
            textBoxArray[4, 0] = textBox21;
            textBoxArray[4, 1] = textBox22;
            textBoxArray[4, 2] = textBox23;
            textBoxArray[4, 3] = textBox24;
            textBoxArray[4, 4] = textBox25;
            
        }
        private void newGame_Click(object sender, EventArgs e)
        {
            Random rand = new Random();
            for (int i = 0; i < 5; i  )
            {
                for (int j = 0; j < 5; j  )
                {
                    textBoxArray[i, j].Text = "";
                    textBoxArray[i, j].ReadOnly = false;
                }
            }
            int randomNum = rand.Next(0, 5);
            textBoxArray[randomNum, randomNum].Text = "1";
        }

        private void ifTextChanged (object sender, EventArgs e)
        {
            // Set textbox with Value 1 to Read Only

            for (int i = 0; i < 5; i  )
            {
                for (int j = 0; j < 5; j  )
                {
                    if (textBoxArray[i, j].Text.Equals("1"))
                    {
                        textBoxArray[i, j].ReadOnly = true;
                    }
 
                }
            }
           
        }

CodePudding user response:

Put this same text changed event handler on all the textboxes:

private void Any_TextChanged(object sender, EventArgs e){
  if(this.Controls.OfType<TextBox>().Any(tb => tb != sender && tb.Text == (sender as TextBox).Text))
   MessageBox.Show((sender as TextBox).Text = "Dupe";
}
  • Related