Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two button(read and write), each read and write has 5 textboxes. I want to compare the value between read and write, if it is match, the textbox will show color green else red color, but it didnt change the color when i reclick the read button

string[] rTextBoxes = { textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text};
string[] wTextBoxes = { textBox15.Text, textBox16.Text, textBox17.Text, textBox18.Text, textBox19.Text};
var totalCount = readTextBoxes.Count();
for (int i = 0; i < totalCount; i++)
{
    if (rTextBoxes[i] != wTextBoxes[i])
    {
        foreach (var control in this.Controls)
        {
            var textBox = control as TextBox;
            if (textBox != null)
            {
                textBox.ReadOnly = false;
            }
        }
        textBox15.BackColor = Color.Red;
        textBox16.BackColor = Color.Red;
        textBox17.BackColor = Color.Red;
        textBox18.BackColor = Color.Red;
        textBox19.BackColor = Color.Red;

    }
    else
    {
        textBox15.BackColor = Color.Green;
        textBox16.BackColor = Color.Green;
        textBox17.BackColor = Color.Green;
        textBox18.BackColor = Color.Green;
        textBox19.BackColor = Color.Green;
    }

}


What I have tried:

I try to use invoke method and make the color empty, but it has no response on the color changed too.

if (textBox.InvokeRequired)
{
    textBox.Invoke((MethodInvoker)delegate
    {
        textBox.BackColor = Color.Empty;
    });
}
Posted
Updated 27-Feb-24 17:27pm
v2
Comments
George Swan 28-Feb-24 2:23am    
It seems to me that you need to break out of the 'for' loop when the first mismatched text is found or else it will be the last text match alone that determines if a colour switch is made

1 solution

Invoke is irrelevant: it is only required when you are using a thread other than the main GUI thread. Invoke "moves the code" back to the GUI thread so it is safe to access controls.

If your colour doesn't change there are several possible reasons - for example if totalCount is zero or negative, the colour will not be changed.
We can't tell without being able to run your code in its entirety - and we can't do that as we have no access to your system.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900