Click here to Skip to main content
15,919,245 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can set the custom colors for color dialog by the code below

C#
MyDialog.CustomColors = new int[]{6916092, 15195440, 16107657, 1836924,
   3758726, 12566463, 7526079, 7405793, 6945974, 241502, 2296476, 5130294,
   3102017, 7324121, 14993507, 11730944,};


But I want to add one custom color one when color dialogbox is opened.

And if I open Another color then only a new custom coor should be added to the second slot

How to do this.
Posted

1 solution


First, your example is taken from MSDN documentation without attribution (including the unnecessary trailing comma in the int [ ] declaration). You MUST always provide attribution (like "from MSDN ColorDialog.CustomColors Property") when you copy code examples. That way I know what you know.



Note that the number of custom color slots in the ColorDialog is limited to 16. Note too that the colors are filled from left to right, top row first.



Set the AllowFullOpen property to false. This keeps control of the custom color slots to your application. Now, each time that a user chooses a color, you can add it to the array that you keep in your application and supply to the ColorDialog.



We are all familiar with the Color structure. BUT when converting a Color to an Int32 you must make an adjustment. The Color structure red, green, and blue components are stored in adjacent bytes in the order ARGB, where A is the alpha value (transparency). But as pointed
out in MSDN documentation



Users can create their own set of custom colors. These colors are
contained in an Int32 composed of the BGR (blue, green, red)
values necessary to create the color.


So the colors supplied to the ColorDialog are constructed differently than the construction of the Color structure.



The following code performs what I think you want. It requires a form, named Form1, with a button named button1.


C#
using System;
using System.Drawing;
using System.Windows.Forms;

namespace CustomColorDialog
    {

    // *************************************************** class Form1

    public partial class Form1 : Form
        {

        const int   MAXIMUM_CUSTOM_COLORS = 16;

        int [ ]     custom_colors = new int [ MAXIMUM_CUSTOM_COLORS ];
        int         custom_colors_count = 0;

        // ***************************************************** Form1

        public Form1 ( )
            {

            InitializeComponent ( );

            for ( int i = 0; ( i < custom_colors.Length ); i++ )
                {
                custom_colors [ i ] = 
                    ( Color.White.ToArgb ( ) & 0x00FFFFFF );
                }

            button1.Click += new EventHandler ( button1_Click );
            }

        // ********************************************* button1_Click

        void button1_Click ( object sender, EventArgs e )
            {
            Color       color = Color.Empty;
            ColorDialog color_dialog = new ColorDialog ( );

            color_dialog.AllowFullOpen = false;
            color_dialog.CustomColors = custom_colors;
            if ( color_dialog.ShowDialog ( ) == DialogResult.OK )
                {
                color = color_dialog.Color;
                if ( custom_colors_count >= custom_colors.Length )
                    {
                    custom_colors_count = 0;
                    }
                button1.BackColor = color;
                int color_value = ( color.B << 16 ) +
                                  ( color.G << 8 ) +
                                  color.R;
                custom_colors [ custom_colors_count++ ] = color_value;
                }
            }

        } // class Form1

    } // namespace CustomColorDialog
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900