How to convert color in VB.NET, Color.FromArgb in code behind using VB.net and class with common Hex value like #ffffff instead of white


 
'This can be used within a class or built as a class, I would recommend it as a class and import it 'when necesary 
    Protected Function getHex(ByVal hex As String) As System.Drawing.Color
        'remove the # at the front
        hex = hex.Replace("#", "")
 
        Dim a As Byte = 255
        Dim r As Byte = 255
        Dim g As Byte = 255
        Dim b As Byte = 255
 
        Dim start As Integer = 0
 
        'handle ARGB strings (8 characters long)
        If hex.Length = 8 Then
            a = Byte.Parse(hex.Substring(0, 2), System.Globalization.NumberStyles.HexNumber)
            start = 2
        End If
 
        'convert RGB characters to bytes
        r = Byte.Parse(hex.Substring(start, 2), System.Globalization.NumberStyles.HexNumber)
        g = Byte.Parse(hex.Substring(start + 2, 2), System.Globalization.NumberStyles.HexNumber)
        b = Byte.Parse(hex.Substring(start + 4, 2),       System.Globalization.NumberStyles.HexNumber)
 
        Return System.Drawing.Color.FromArgb(a, r, g, b)
    End Function
 
'Then assign it to any of your controls to ForeColor or BackColor:
        btn.ForeColor = getHex("#9ecc4f")
 
 
Tags: Color, Drawing, Hex, VB
Last update:
2015-04-17 21:17
Author:
Mark
Revision:
1.0
Average rating:0 (0 Votes)

You cannot comment on this entry

Chuck Norris has counted to infinity. Twice.

Records in this category

Tags