# Discussion: Coloring a textbox with contain the maximum value of a two-dimensional array

4 messages from 4 displayed.

2022/01/21 08:22:21

Replies to Maria Rymar

**David Capka:**11/20/2019 8:20

Hi Maria, your code makes sense to me. Which line is causing the error and what's the error message?

Replies to David Capka

**Maria Rymar:**11/20/2019 8:27

Hi! Look, there is no error, but the program works incorrectly. I want to find the Textbox with the maximum value and colour it. But the program colours 3 and more textboxes and i dont now why. I need only one coloured textbox with the max value.

Replies to Maria Rymar

**David Capka:**11/22/2019 11:42
Edited 11/22/2019 11:43

I see The problem is you color the TextBox every time you find a new maximal value. It takes multiple guesses to find the maximal value and you can be sure there is no larger value only when both loops finish.

You need to use 2 loops to find the maximal value and then 2 different loops to color every TextBox having this value (coz there can be more of them). Your code should look something like this:

```
// find the maximum
int max = Convert.ToInt32(t[0, 0].Text);
for (i = 0; i < x; i++)
for (j = 0; j < y; j++)
if (Convert.ToInt32(t[i, j].Text) > max)
max = Convert.ToInt32(t[i, j].Text);
// write the maximum
label1.Text = max.ToString();
// color all cells with the maximum
for (i = 0; i < x; i++)
for (j = 0; j < y; j++)
if (Convert.ToInt32(t[i, j].Text) == max)
t[i, j].BackColor = Color.Red;
```

4 messages from 4 displayed.

Maria Rymar