How to convert color name to integer

Posted on 2007-09-30
Last Modified: 2008-01-09
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}

Question by:gem56

    Author Comment

    Forgot to include a secondary question.
    How can I retrieve the CustomClors if I want to save them for later?

    LVL 14

    Expert Comment

           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)
    LVL 10

    Expert Comment

        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)

    Author Comment

    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?
    LVL 10

    Expert Comment

    You simply use the color name

    MyDialog.CustomColors  = Color.FromName("Azure")

    Author Comment

    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?
    LVL 10

    Accepted Solution

    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

        End Sub

    Author Comment

    Hi ericwong27,
    That's done the trick. Thanks a lot.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now