1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
class ListViewColorManger
{
const int init = 50;
static int i = init;
static bool IsInitialized = false;
static System.Array colorsArray = Enum.GetValues(typeof(KnownColor));
static KnownColor[] allColors;
static Dictionary<string, Color> ItemsColor = new Dictionary<string, Color>();
static Dictionary<string, List<Color>> CellsColor = new Dictionary<string, List<Color>>();
static void InitailiseListViewColorManger()
{
allColors = new KnownColor[colorsArray.Length];
Array.Copy(colorsArray, allColors, colorsArray.Length);
IsInitialized = true;
}
public static void GetColorForListView(ref ListView listView1)
{
if (!IsInitialized)
{
InitailiseListViewColorManger();
IsInitialized = true;
}
foreach (ListViewItem item in listView1.Items)
{
string Nexpedition = item.SubItems[0].Text;
if (ItemsColor.ContainsKey(Nexpedition))
{
item.BackColor = ItemsColor[Nexpedition];
item.SubItems[0].ForeColor = GetColorFromCell(item.BackColor, Nexpedition, CellsColor);
if (CellsColor.ContainsKey(Nexpedition))
{
CellsColor[Nexpedition].Add(item.SubItems[0].ForeColor);
}
else
{
List<Color> list = new List<Color>();
list.Add(item.SubItems[0].ForeColor);
CellsColor.Add(Nexpedition, list);
}
}
else
{
Color itColor = getColorForItem();
Color celColor = GetColorFromCell(item.BackColor, Nexpedition, CellsColor);
item.BackColor = itColor;
item.SubItems[0].ForeColor = celColor;
ItemsColor.Add(Nexpedition, item.BackColor);
List<Color> list = new List<Color>();
list.Add(item.SubItems[0].ForeColor);
CellsColor.Add(Nexpedition, list);
}
}
}
static Color getColorForItem()
{
if (i == allColors.Length)
i = init;
Color color = Color.FromKnownColor(allColors[i]);
i++;
return color;
}
static Color GetColorFromCell(Color Coloritem, string key, Dictionary<string, List<Color>> CellsColor)
{
Color currentColor = Color.FromKnownColor(allColors[0]);
for (int j = 0; j < allColors.Length; j += 10)
{
currentColor = Color.FromKnownColor(allColors[j]);
if (currentColor == Coloritem)
continue;
else if (CellsColor.ContainsKey(key))
{
if (CellsColor[key].Contains(currentColor))
continue;
else
return currentColor;
}
else
return currentColor;
}
return currentColor;
}
} |
Partager