A Simplified Color Guide in C#

Jayant M. contributed with a programgenerating all the colors supported in C# by their names. Here is asimplified version of this program. To loop throughout all the knowncolor's names I use GetName method of the Enum struct and to get the corresponding Color object I use the FromName method of the Color class.

Download SimplifiedColorGuide.zip

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ColorGuide
{
/// <summary>
/// Summary description for Form1.
/// </summary>

public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.ToolTip toolTip1;

private Panel panel1 = new Panel();

public Form1()
{
//
// Required for Windows Form Designer support
//

InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//

// Set up the delays for the ToolTip.
toolTip1.AutoPopDelay = 5000;
toolTip1.InitialDelay = 1000;
toolTip1.ReshowDelay = 500;
// Force the ToolTip text to be displayed whether or not the form is active.
toolTip1.ShowAlways = true;
// Initialize the Panel control.
panel1.Location = new Point(ClientRectangle.Left + 5,ClientRectangle.Top + 5);
panel1.Size = new Size(ClientRectangle.Right-5, ClientRectangle.Bottom-5);
panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
// Add the Panel control to (inside) the form.
this.Controls.Add(panel1);

int ystart = ClientRectangle.Top;
// loop throughout all the known colors
foreach(string clrName in Enum.GetNames(typeof(KnownColor)))
{
Label lbl = new Label();
lbl.Size = new Size(panel1.Width-20, 20);
lbl.Font = new System.Drawing.Font("Comic Sans MS",10,FontStyle.Bold);
lbl.Text = clrName;

lbl.Location = new Point(ClientRectangle.Left,ystart);
Color backcolor = Color.FromName(clrName);
lbl.BackColor = backcolor;
// set the tooltip
toolTip1.SetToolTip(lbl, clrName);
Color forecolor = Color.FromArgb(255-backcolor.R,255-backcolor.G,255-backcolor.B);
lbl.ForeColor = forecolor;
lbl.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
// Console.WriteLine("{0},{1},{2},{3}<->{4},{5},{6},{7}",
// backcolor.A, backcolor.R, backcolor.G, backcolor.B,
// forecolor.A, forecolor.R, forecolor.G, forecolor.B);

// Add the Label control to (inside) the Panel.
panel1.Controls.Add(lbl);
if((lbl.Location.Y > panel1.Location.Y))
{
panel1.AutoScroll = true;
}
ystart += 20;
}
this.Size = new Size(315, 300);
this.Text = "A Color Guide – JAYANT Simplified";
this.MaximizeBox = false;
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.StartPosition = FormStartPosition.CenterScreen;
}

/// <summary>
/// Clean up any resources being used.
/// </summary>

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
/// </summary>

private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
//
// Form1
//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "Form1";
this.Text = "Form1";

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>

[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}

Happy coding,
Csaba

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!