Click here to Skip to main content
15,885,839 members
Articles / Programming Languages / C#

Button derived color selection control in C#

Rate me:
Please Sign up or sign in to vote.
4.21/5 (15 votes)
22 Dec 20031 min read 82.4K   1.2K   31  
Button derived color selection control similar to the one used in MS Office applications.
using System ;
using System.Drawing ;
using System.ComponentModel ;
using System.Windows.Forms ;

namespace ColorButtonDemo
{
	public class Form1 : System.Windows.Forms.Form
	{
        private System.ComponentModel.Container components = null ;

        private ColorButton.ColorButton buttonColor ;
        private System.Windows.Forms.GroupBox groupBox1 ;
        private System.Windows.Forms.CheckBox checkBoxAuto ;
        private System.Windows.Forms.CheckBox checkBoxMore ;
        private System.Windows.Forms.GroupBox groupBox2 ;
        private System.Windows.Forms.Label labelSelected ;

		public Form1()
		{
			InitializeComponent() ;

            buttonColor.Color = Color.Red ;
            labelSelected.ForeColor = Color.Red ;
        }

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

			base.Dispose( disposing ) ;
		}

		#region Vom Windows Form-Designer generierter Code
		private void InitializeComponent()
		{
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.checkBoxMore = new System.Windows.Forms.CheckBox();
            this.checkBoxAuto = new System.Windows.Forms.CheckBox();
            this.buttonColor = new ColorButton.ColorButton();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.labelSelected = new System.Windows.Forms.Label();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.checkBoxMore);
            this.groupBox1.Controls.Add(this.checkBoxAuto);
            this.groupBox1.Location = new System.Drawing.Point(192, 16);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(120, 72);
            this.groupBox1.TabIndex = 1;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "Options";
            // 
            // checkBoxMore
            // 
            this.checkBoxMore.Checked = true;
            this.checkBoxMore.CheckState = System.Windows.Forms.CheckState.Checked;
            this.checkBoxMore.Location = new System.Drawing.Point(16, 40);
            this.checkBoxMore.Name = "checkBoxMore";
            this.checkBoxMore.Size = new System.Drawing.Size(88, 24);
            this.checkBoxMore.TabIndex = 1;
            this.checkBoxMore.Text = "More Colors";
            this.checkBoxMore.CheckedChanged += new System.EventHandler(this.OnMoreColors);
            // 
            // checkBoxAuto
            // 
            this.checkBoxAuto.Checked = true;
            this.checkBoxAuto.CheckState = System.Windows.Forms.CheckState.Checked;
            this.checkBoxAuto.Location = new System.Drawing.Point(16, 16);
            this.checkBoxAuto.Name = "checkBoxAuto";
            this.checkBoxAuto.Size = new System.Drawing.Size(80, 24);
            this.checkBoxAuto.TabIndex = 0;
            this.checkBoxAuto.Text = "Automatic";
            this.checkBoxAuto.CheckedChanged += new System.EventHandler(this.OnAutomatic);
            // 
            // buttonColor
            // 
            this.buttonColor.Automatic = "Automatic";
            this.buttonColor.Color = System.Drawing.Color.Transparent;
            this.buttonColor.Location = new System.Drawing.Point(24, 24);
            this.buttonColor.MoreColors = "More Colors...";
            this.buttonColor.Name = "buttonColor";
            this.buttonColor.Size = new System.Drawing.Size(64, 23);
            this.buttonColor.TabIndex = 0;
            this.buttonColor.Changed += new System.EventHandler(this.OnChanged);
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.labelSelected);
            this.groupBox2.Location = new System.Drawing.Point(192, 104);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(120, 56);
            this.groupBox2.TabIndex = 2;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "Changed Event";
            // 
            // labelSelected
            // 
            this.labelSelected.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.labelSelected.Location = new System.Drawing.Point(16, 24);
            this.labelSelected.Name = "labelSelected";
            this.labelSelected.Size = new System.Drawing.Size(96, 23);
            this.labelSelected.TabIndex = 0;
            this.labelSelected.Text = "Selected Color";
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(330, 208);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.buttonColor);
            this.Controls.Add(this.groupBox1);
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "ColorButton Demo";
            this.groupBox1.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            this.ResumeLayout(false);

        }
		#endregion

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

        private void OnAutomatic( object sender, System.EventArgs e )
        {
            if( checkBoxAuto.Checked )
                buttonColor.Automatic = "Automatic" ;
            else
                buttonColor.Automatic = "" ;
        }

        private void OnMoreColors( object sender, System.EventArgs e )
        {
            if( checkBoxMore.Checked )
                buttonColor.MoreColors = "More Colors..." ;
            else
                buttonColor.MoreColors = "" ;
        }

        private void OnChanged( object sender, System.EventArgs e )
        {
            if( buttonColor.Color == Color.Transparent )
            {
                labelSelected.Text = "Automatic Color" ;
                labelSelected.ForeColor = Color.Black ;
            }
            else
            {
                labelSelected.Text = "Selected Color" ;
                labelSelected.ForeColor = buttonColor.Color ;
            }
        }
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Austria Austria
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions