Here recently I have been working more and more with Visual Studio again, and more specifically the Color Picker / Color Dialog control. For my purposes, I wanted the user to pick a color, and that color to then populate a text box in the form of an HTML compliant hex value, in other words, I need the result from the color picker as a string. Now, in VB.NET 2010 which is what I am using, the result from the color picker is in a format that is primed and ready to apply to a control, to change its fore or back round color. It does not give you a value in string format. So, I had to do a good bit of searching and work to figure this out.
Basically, what I ended up with was a function that you run the color picker response or value through. It’s pretty simple really:
Public Function Color2Html(ByVal MyColor As Color) As String Return "#" & MyColor.ToArgb().ToString("x").Substring(2).ToUpper End Function
That’s it, that will return to you a hex color code formatted for HTML use. Just in case you are unsure of exactly how to use the function, this is the code I use with my color picker control (I use a standard button to fire it off with the result going into a text box):
Private Sub btnColor1_Click(sender As System.Object, e As System.EventArgs) Handles btnColor1.Click Dim NewColor As New ColorDialog() NewColor.AllowFullOpen = True NewColor.ShowHelp = True If (NewColor.ShowDialog() = Windows.Forms.DialogResult.OK) Then txtColor.Text = Color2Html(NewColor.Color) End If End Sub
That’s all there is too it. I hope this helps you get your string data from your Color Dialog / Color Picker control. Enjoy!