1

I want implement a basic ColorPicker in my app, what I need is that when the user click on a TextBox appear the ColorPicker and when the user choice the color the TextBox BackColor get the color selected, also I want display in the TextBox the value of the selected color. This is what I do actually:

Private Sub resource_colore_TextChanged(sender As Object, e As EventArgs) Handles resource_colore.Click

    Dim cDialog As New ColorDialog
    Dim conv As New ColorConverter

    cDialog.Color = resource_colore.BackColor   

    If (cDialog.ShowDialog() = DialogResult.OK) Then
        resource_colore.BackColor = cDialog.Color 
        Dim hex_color As String = Hex(cDialog.Color)
        resource_colore.Text = hex_color
    End If

End Sub

Now the problem's I get this exception:

can't convert the argument 'Number' in the 'Color' type

on this line:

Dim hex_color As String = Hex(cDialog.Color)

what exactly means? How I can fix?

Dillinger
  • 1,823
  • 4
  • 33
  • 78
  • 1
    Possible duplicate of [Convert System.Drawing.Color to RGB and Hex Value](http://stackoverflow.com/questions/2395438/convert-system-drawing-color-to-rgb-and-hex-value) – sstan Dec 12 '15 at 21:01
  • 1
    Color is a structure (Type) so you cant convert it directly. You need to convert the R G B values something like `String.Format("#{0:X2}{1:X2}{2:X2}", myClr.R, myClr.G, myClr.B)` – Ňɏssa Pøngjǣrdenlarp Dec 12 '15 at 21:03
  • I saw this but System.Drawing.Color isn't different again ColorDialog? – Dillinger Dec 12 '15 at 21:04
  • The error message seems quite descriptive. The function `Hex` expects a number and you are inputting a completely different thing (a whole color). And yes, `System.Drawing.Color` is different than `ColorDialog` but it is exactly the same than `ColorDialog.Color`. Shouldn't you make a small debugging/understanding/research effort before asking here? – varocarbas Dec 12 '15 at 21:06
  • Thank you Plutonix but a personal question: are you a bot? In each my vb.net question there's your own answer XD thanks again, have a nice day :) – Dillinger Dec 12 '15 at 21:09
  • 1
    You can also use `ColorTranslator.ToHtml(Color.FromArgb(colorDialog.Color.ToArgb()))` – Reza Aghaei Dec 12 '15 at 21:34
  • @RezaAghaei why do you convert a color to a number, back to a color again? Just use the color directly: `ColorTranslator.ToHtml(cDialog.Color)` – Visual Vincent Dec 13 '15 at 18:53
  • @VisualVincent Try it for named colors like Color.Red and see the result :) – Reza Aghaei Dec 13 '15 at 18:54
  • @RezaAghaei well... Does the color dialog's colors really account for that? O.o – Visual Vincent Dec 13 '15 at 18:58
  • @VisualVincent Using named colors is what `ColorTranslator` do, and using such workaround cause the color translator return hex color value. – Reza Aghaei Dec 13 '15 at 19:41

1 Answers1

3

As suggested by Plutonix the answer is simple, I fix like this:

Private Sub resource_colore_TextChanged(sender As Object, e As EventArgs) Handles resource_colore.Click

    Dim cDialog As New ColorDialog
    Dim conv As New ColorConverter

    cDialog.Color = resource_colore.BackColor  

    If (cDialog.ShowDialog() = DialogResult.OK) Then
        resource_colore.BackColor = cDialog.Color 
        Dim hex_color As String = String.Format("#{0:X2}{1:X2}{2:X2}", cDialog.Color.R, cDialog.Color.G, cDialog.Color.B)
        resource_colore.Text = hex_color
    End If

End Sub

Thanks to everyone

Dillinger
  • 1,823
  • 4
  • 33
  • 78