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.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace CustomColorDialog
{
public partial class Form1 : Form
{
const int MAXIMUM_CUSTOM_COLORS = 16;
int [ ] custom_colors = new int [ MAXIMUM_CUSTOM_COLORS ];
int custom_colors_count = 0;
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 );
}
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;
}
}
}
}