1

I am trying to read BackColor of cells with the below function,that returns me double type values. I need to read the values for 25 Cells, but it takes too much time to read all these cells (to exclude the code). And then I need to set that color as BackColor of TextBoxes. I am using Interop Library. Is it possible to reduce this delay of reading the BackColor of the cells?

The problem is accessing the cell. GetColor() its not the problem. Its just that it takes too much time to read values from the cell.

var colorCode = ws.Cells[i, objectHeading].Interior.Color;
public Color GetColor(int i, double colorCode)
{
    if (colorCode == 12566463)
        return Color.LightGray;
    else if (colorCode == 65535)
        return Color.Yellow;
    else if (colorCode == 3394611 || colorCode == 5296274)
        return Color.LightGreen;
    else if (colorCode == 49407)
        return Color.Orange;
    else if (colorCode == 15773696)
        return Color.LightBlue;
    else
        return Color.White;
}
  • Have you tried using the `switch` statement? More info [here](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/switch). – ShawnOrr Apr 22 '21 at 15:07
  • @Rron Frangu: Try to measure code performance: what takes too much time, accessing the cell or the `GetColor()` method? If it is the `GetColor()` then try to check using the `HashTable`. The `switch-case` translated by compiler to the same `if-else`: therefore does not help much. See [Is there any significant difference between using if/else and switch-case in C#?](https://stackoverflow.com/q/395618/6630084) – Jackdaw Apr 22 '21 at 17:10
  • Make sure to not do page updates as you change the colors. `Globals.ThisAddIn.Application.ScreenUpdating = false; ` – ΩmegaMan Apr 22 '21 at 21:41
  • The problem is accessing the cell. >GetColor its not the real problem. Its just that it takes too much time to read values from the cell. – Rron Frangu Apr 23 '21 at 08:12

0 Answers0