0

In my winforms application I have a ColorDialog control and when the user select a color, I save the Name of the color object. For a custom color value starts with FF ex: fffdfcc8 (This is a light yellow / cream color)

Some where later I want to get to the color object from the color name string, fffdfcc8 to a system.drawing.color object. How can I do this???

        Dim dlgColor As New ColorDialog()
        dlgColor.AllowFullOpen = True
        dlgColor.AnyColor = True
        If dlgColor.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            DgView.CurrentCell.Text = dlgColor.Color.Name 
        End If     

How to create color object based on the name value in DgView.CurrentCell.Text

UPDATE2: Adding # in front of the custom name value and using the System.Drawing.ColorTranslator.FromHtml("#" & color) does get the job done. Not sure what to do next since to me it was not that obvious...leave it up to the moderator to decide

UPDATE: This is not a duplicate since the name value is not a hex value.

Ashkan Mobayen Khiabani
  • 33,575
  • 33
  • 102
  • 171
glant
  • 479
  • 6
  • 19
  • Possible duplicate of [How to get Color from Hexadecimal color code using .NET?](http://stackoverflow.com/questions/2109756/how-to-get-color-from-hexadecimal-color-code-using-net) – Phiter Nov 21 '16 at 21:35
  • @Phiter Its not a duplicate since the value I have is not a hex value that starts with a #. I also don't have the convertor library available – glant Nov 21 '16 at 21:44
  • Just add a hash – Phiter Nov 21 '16 at 21:44
  • 1
    You can do this: `System.Drawing.ColorTranslator.FromHtml("# " + color);` – Phiter Nov 21 '16 at 21:45
  • @PhiterFernandes Thanks that did it, I did try the html function but did not think of adding the #. Thanks again – glant Nov 21 '16 at 21:50

1 Answers1

0

Prefix # to the custom name and use it like this should work. As indicated in the comments and updates. Thanks to Phiter Fernandes for the help.

   Dim strColor = DgView.CurrentCell.Text
   System.Drawing.ColorTranslator.FromHtml("#" & strColor)
glant
  • 479
  • 6
  • 19