[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Let the user select colors from menus in C#

[Let the user select colors from menus in C#]

I use this technique fairly often, so I'm making a post partly so I can copy and paste its code into future programs.

The Settings menu's Background Color command uses its Image property to display a sample of the currently selected color. You can see that sample in the picture on the right.

When you invoke that menu item, the program displays a ColorDialog where you can select a new color. It then updates the sample displayed in the menu item.

When it starts, the program uses the folllowing code to get ready.

// Initialize the background color menu item. private void Form1_Load(object sender, EventArgs e) { // Add the background color to the dialog's custom colors. // Use light colors plus the initial background // color for the dialog's custom colors. int bg_color = BackColor.B * 256 * 256 + BackColor.G * 256 + BackColor.R; int[] bg_colors = { bg_color, 0xFFC0C0, 0xFFE0C0, 0xFFFFC0, 0xC0FFC0, 0xC0FFFF, 0xC0C0FF, 0xFFC0FF, 0xE0E0E0, 0xFF8080, 0xFFC080, 0xFFFF80, 0x80FF80, 0x80FFFF, 0x8080FF, 0xFF80FF }; int[] bg_colors = { bg_color }; cdBgColor.CustomColors = bg_colors; // Display the current color on the menu item. SetBgColor(BackColor); }

This code first creates an array holding the initial background color plus a collection of light colors. Each color's value holds its blue, green, and red color components in the form 0xBBGGRR. After it has dfined the color array, the program uses it to set the custom colors displayed by its ColorDialog. (You'll see shortly why we need to at least set the first color to the form's initial background color.)

Next, the code calls the following SetBgColor method to update the form's background color and the menu item's color sample.

// Set the background color and update the men item. private void SetBgColor(Color color) { // Use the color. BackColor = color; // Make an image to show a color sample. const int wid = 24; // Image width. Bitmap bm = new Bitmap(wid, wid); using (Graphics gr = Graphics.FromImage(bm)) { gr.Clear(color); gr.DrawRectangle(Pens.Black, 0, 0, wid - 1, wid - 1); mnuSettingsBgColor.Image = bm; } }

The method first sets the form's background color. It then creates a 24 x 24 pixel bitmap. It clears the bitmap with the new background color and then outlines it with a black rectangle to make the color sample stand out from the menu item's background color.

[Let the user select colors from menus in C#]

The following code executes when you invoke the Background Color menu item.

private void mnuSettingsBgColor_Click(object sender, EventArgs e) { // (This won't work if the color isn't on the dialog.) // Show the current color. cdBgColor.Color = BackColor; // Display the dialog. if (cdBgColor.ShowDialog() == DialogResult.OK) { SetBgColor(cdBgColor.Color); } }

This code sets the color dialog's Color property to the current background color. This won't work if that color is not available in the dialog. That's why the Form_Load event handler adds the form's initial background color to the color dialog. If that color is in the dialog, then the program can select that color the first time you invoke the men command.

Next, the code displays the dialog and, if you pick a color and click OK, the program calls SetBgColor to use the selected color.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.