Display a series of PictureBoxes containing a standard palette of colors in C#

The MakeColorPalette method shown below builds an array of PictureBoxes with BackColor properties set to a standard color palette.

// Make a color palette on the given parent.
private void MakeColorPalette(Control parent,
 int x, int y, System.EventHandler event_handler)
{
Color[] colors =
{
Color.White,
Color.FromArgb(255, 255, 192, 192),
Color.FromArgb(255, 255, 224, 192),
Color.FromArgb(255, 255, 255, 192),
Color.FromArgb(255, 192, 255, 192),
Color.FromArgb(255, 192, 255, 255),
Color.FromArgb(255, 192, 192, 255),
Color.FromArgb(255, 255, 192, 255),
Color.FromArgb(255, 224, 224, 224),
Color.FromArgb(255, 255, 128, 128),
Color.FromArgb(255, 255, 192, 128),
Color.FromArgb(255, 255, 255, 128),
Color.FromArgb(255, 128, 255, 128),
Color.FromArgb(255, 128, 255, 255),
Color.FromArgb(255, 128, 128, 255),
Color.FromArgb(255, 255, 128, 255),
Color.Silver,
Color.Red,
Color.FromArgb(255, 255, 128, 0),
Color.Yellow,
Color.Lime,
Color.Cyan,
Color.Blue,
Color.Fuchsia,
Color.Gray,
Color.FromArgb(255, 192, 0, 0),
Color.FromArgb(255, 192, 64, 0),
Color.FromArgb(255, 192, 192, 0),
Color.FromArgb(255, 0, 192, 0),
Color.FromArgb(255, 0, 192, 192),
Color.FromArgb(255, 0, 0, 192),
Color.FromArgb(255, 192, 0, 192),
Color.FromArgb(255, 64, 64, 64),
Color.Maroon,
Color.FromArgb(255, 128, 64, 0),
Color.Olive,
Color.Green,
Color.Teal,
Color.Navy,
Color.Purple,
Color.Black,
Color.FromArgb(255, 64, 0, 0),
Color.FromArgb(255, 128, 64, 64),
Color.FromArgb(255, 64, 64, 0),
Color.FromArgb(255, 0, 64, 0),
Color.FromArgb(255, 0, 64, 64),
Color.FromArgb(255, 0, 0, 64),
Color.FromArgb(255, 64, 0, 64),
};
const int num_rows = 6;
const int num_columns = 8;
const int pic_width = 20;
const int pic_height = 20;
const int spacing = 4;

int row_y = y;
for (int row = 0; row < num_rows; row++)
{
int column_x = x;
for (int column = 0; column < num_columns; column++)
{
PictureBox pic = new PictureBox();
pic.Parent = parent;
pic.Click += event_handler;
pic.BackColor = colors[row * num_columns + column];
pic.Size = new Size(pic_width, pic_height);
pic.Location = new Point(column_x, row_y);
pic.BorderStyle = BorderStyle.Fixed3D;
column_x += pic_width + spacing;
}
row_y += pic_height + spacing;
}
}

The code starts by defining an array containing the colors that should be in the palette. It then loops through the array building PictureBoxes. The code sets each PictureBox's:

  • Parent to the control that should contain the PictureBox. Note that a form is a type of control so the program can pass that for the method's parent parameter.
  • Click event handler. This is the delegate passed in by the calling code.
  • BackColor
  • Size
  • Location
  • BorderStyle

The code then updates the position of the next PictureBox and continues the loop.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.