How to convert color name to integer

Hi,
I'm trying to set some CustomColors in the ColorDialog control and I con do that without any problems however I'm having a bit of a problem converting color-name to integer, please see code below:

   Dim Cust1 As Integer = Color.FromName("Azure").ToArgb
   Dim Cust2 As Integer = Color.FromName(AliceBlue).ToArgb
   MyDialog.CustomColors = New Integer() {Cust1, Cust2}

Thanks,
     Michael
gem56Asked:
Who is Participating?
 
ericwong27Connect With a Mentor Commented:
I think below sample can be help. You need to use RGB instead of integer

 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim myDialog As New System.Windows.Forms.ColorDialog()

        'Set Custom Colors to be shown
        myDialog.CustomColors = New Integer() {RGB(Color.FromName("Red").R, Color.FromName("Red").G, Color.FromName("Red").B), _
                                               RGB(Color.FromName("Yellow").R, Color.FromName("Yellow").G, Color.FromName("Yellow").B), _
                                               RGB(Color.FromName("Green").R, Color.FromName("Green").G, Color.FromName("Green").B)}

        'Show the Dialog and get the returned result
        If myDialog.ShowDialog = DialogResult.OK Then
            'Do something if the result matches what we want
            Me.BackColor = myDialog.Color
        End If

        'reset everything to the defaults
        myDialog.Reset()


    End Sub
0
 
gem56Author Commented:
Forgot to include a secondary question.
How can I retrieve the CustomClors if I want to save them for later?

/Michael
0
 
Jai STech ArchCommented:
       Dim i As Integer = Color.FromName("Blue").ToArgb
        Dim c As Color = Color.FromArgb(i)

argb will return teh COLOR in ARGB format...
when you execute the lines the C will have the color blue...properly...

for getting the custom colors you can specifu the ARGB value by urself in the color.FromARGB(alpha,red,green,blue)
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
ericwong27Commented:
    You still able to base on color name. Both sample produce same result

      ' Base on color name
        Dim colorName As String = Color.FromName("AliceBlue").Name
        Me.BackColor = Color.FromName(colorName)

        ' Base on RGB
        Dim colorArgb As Integer = Color.FromName("AliceBlue").ToArgb
        Me.BackColor = Color.FromArgb(colorArgb)
0
 
gem56Author Commented:
Hi guys,
Thanks for your responses however I don't see any difference between what you're suggesting and what I'm already doing:

Dim Cust1 As Integer = Color.FromName("Azure").ToArgb                    (me)
Dim i As Integer = Color.FromName("Blue").ToArgb                               (jaiganeshsrinivasan)
Dim colorArgb As Integer = Color.FromName("AliceBlue").ToArgb        (ericwong27)

My ultimate problem is that I'm trying to initialise ColorDialog.CustomColors property using color names rather than numeric constants. I've tried the following but that too doesn't work:
        Dim iTmp(1) As Integer
        iTmp(0) = Color.FromName("red").A & Color.FromName("red").R & Color.FromName("red").G & Color.FromName("red").B
        iTmp(1) = Color.FromName("blue").A & Color.FromName("blue").R & Color.FromName("blue").G & Color.FromName("blue").B
        MyDialog.CustomColors = iTmp

Any more ideas?
/Michael
0
 
ericwong27Commented:
You simply use the color name

example:
MyDialog.CustomColors  = Color.FromName("Azure")
0
 
gem56Author Commented:
When I try to do that I get the following error "Value of type 'System.Drawing.Color' cannot be converted to '1-dimensional array of integer'".

That is why I tried converting Color to Argb but that too didn't work. Were you able to try that statement in your system?
0
 
gem56Author Commented:
Hi ericwong27,
That's done the trick. Thanks a lot.
/Michael
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.