• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3507
  • Last Modified:

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
0
gem56
Asked:
gem56
  • 4
  • 3
1 Solution
 
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
 
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
ericwong27Commented:
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:
Hi ericwong27,
That's done the trick. Thanks a lot.
/Michael
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now