I'm trying to get the tab colors of excel sheets to treeview nodes.backColor, but i'm not getting the correct colors and i'm not sure way (red is blue, blue is orange...). Please let me know what i'm doing wrong.
Sub sheets_loadToTreeView()
Dim WB As Excel.Workbook = Globals.ThisAddIn.Application.ActiveWorkbook
Dim SH As Excel.Worksheet
Dim dSHeets As New Dictionary(Of String, String), dKey, tabColor
Dim dColors As New Dictionary(Of String, Color)
Dim mainNode As TreeNode, colorNode As TreeNode, shNode As TreeNode, MyNode1() As TreeNode
Dim colorname As String
For Each SH In WB.Worksheets
tabColor = color_integerToColor(SH.Tab.Color)
dSHeets.Add(SH.Name.ToString(), SH.Tab.Color)
If Not dColors.ContainsKey(SH.Tab.Color) Then
dColors.Add(SH.Tab.Color, tabColor)
End If
Next
With Me.TreeView1
.CheckBoxes = True
.Nodes.Clear()
mainNode = .Nodes.Add("sheets", "Sheets")
For Each dKey In dColors.Keys
colorName = "------------------"
colorNode = mainNode.Nodes.Add(dKey, colorName)
colorNode.Tag = "color"
colorNode.BackColor = dColors(dKey)
colorNode.ForeColor = dColors(dKey)
Next
For Each dKey In dSHeets.Keys
MyNode1 = Me.TreeView1.Nodes.Find(dSHeets(dKey), True)
If UBound(MyNode1) >= 0 Then
shNode = MyNode1(0).Nodes.Add(dKey, dKey)
shNode.Tag = "sheet"
End If
Next
.ExpandAll()
'MyNode.FirstNode.EnsureVisible()
End With
End Sub
Public Function color_toInteger(ByVal C As Color) As Integer
Return C.ToArgb
End Function
Public Function color_integerToColor(ByVal colorValue As Integer) As Color
Return Color.FromArgb(colorValue)
End Function
Thanks @jimi, I did have my variable type set incorrectly, but that was not the problem. The problem was that Red and Blue were indeed reversed, so I needed the break down the color into RGB and then reverser the Blue and Red position.
Public Function color_integerToColor(ByVal colorValue As Integer) As Color
'Return Color.FromArgb(colorValue,)
'SOURCE: https://social.technet.microsoft.com/wiki/contents/articles/17432.vb-net-how-to-convert-a-32-bit-integer-into-a-color.aspx
Dim Bytes As Byte() = BitConverter.GetBytes(colorValue)
Dim Alpha As Byte = Bytes(3)
Dim Red As Byte = Bytes(0) '2
Dim Green As Byte = Bytes(1)
Dim Blue As Byte = Bytes(2) '0
Return Color.FromArgb(Alpha, Red, Green, Blue)
End Function