Dillinger
Dillinger

Reputation: 1903

How to convert ColorDialog result as hex?

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?

Upvotes: 1

Views: 2556

Answers (1)

Dillinger
Dillinger

Reputation: 1903

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

Upvotes: 3

Related Questions