Loading custom colors into ColorDialog doesn't work properly. For example, Antique White comes out Black.

Howie Walter 21 Reputation points
2022-01-25T16:52:04.777+00:00

I am trying to load some custom colors into a ColorDialog box before it opens, but I can't make it work. The colors come out wrong, or black.

The code is this:

Private Sub FormBackground_Click(sender As Object, e As EventArgs) Handles btnFormBackground.Click
ColorDialog1.AnyColor = True

    Dim colors(4) As Integer

    colors(0) = Math.Abs(Color.Teal.ToArgb())
    colors(1) = Math.Abs(Color.AntiqueWhite.ToArgb())
    colors(2) = Math.Abs(Color.Brown.ToArgb())
    colors(3) = Math.Abs(Color.Aquamarine.ToArgb())
    colors(4) = Math.Abs(Color.BlanchedAlmond.ToArgb())


    ColorDialog1.CustomColors = colors
    If ColorDialog1.ShowDialog = DialogResult.OK Then
        My.Settings.FormBackColor = ColorDialog1.Color
        My.Forms.Main.ColorChanged()
    End If

End Sub

Can anyone explain what I'm doing wrong?

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,681 Reputation points
    2022-01-25T19:09:26.667+00:00

    You can do :

                colors(0) = ColorTranslator.ToOle(Color.Teal)
                colors(1) = ColorTranslator.ToOle(Color.AntiqueWhite)
                colors(2) = ColorTranslator.ToOle(Color.Brown)
                colors(3) = ColorTranslator.ToOle(Color.Aquamarine)
                colors(4) = ColorTranslator.ToOle(Color.BlanchedAlmond)
    
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Viorel 122.6K Reputation points
    2022-01-25T17:17:05.973+00:00

    If this works:

    colors(0) = ((Color.Teal.B << 8) Or Color.Teal.G) << 8 Or Color.Teal.R
    

    then do it similarly for other colours.

    The BGR format is required in documentation for ColorDialog.CustomColors.

    0 comments No comments

  2. Howie Walter 21 Reputation points
    2022-01-25T18:41:16.49+00:00

    I appreciate the reply, but that doesn't work either. I tried this:

    colors(0) = ((Color.Teal.B << 8) Or Color.Teal.G) << 8 Or Color.Teal.R
    colors(1) = ((Color.Red.B << 8) Or Color.Red.G) << 8 Or Color.Red.R
    colors(2) = ((Color.Blue.B << 8) Or Color.Blue.G) << 8 Or Color.Blue.R

    But Teal came out as maroon, Blue came out as red. However Red came out correctly.

    With some of the colors in Color, for example, AntiqueWhite, the compiler says "Or isn't defined for types byte and color."


  3. Howie Walter 21 Reputation points
    2022-01-25T19:01:57.397+00:00

    Correction, I had a typo and that's why I got the compiler error. But the colors still don't display correctly. They are displaying as either maroon or red for those I have tried so far

    0 comments No comments

  4. Howie Walter 21 Reputation points
    2022-01-25T19:48:02.897+00:00

    Yes. That works! Thanks. I also found a brute force method.... take the Hex value, drop the first byte, and with the remaining 3 bytes, swap the first and last byte.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.