Mega Code Archive

 
Categories / C# / GUI Windows Form
 

Change the background and text colors of a form using Color Dialog

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;    public class ShowColorsComplex : System.Windows.Forms.Form    {       private System.Windows.Forms.Button backgroundColorButton;       private System.Windows.Forms.Button textColorButton;       public ShowColorsComplex()       {          InitializeComponent();       }       private void InitializeComponent()       {          this.backgroundColorButton = new System.Windows.Forms.Button();          this.textColorButton = new System.Windows.Forms.Button();          this.SuspendLayout();          //           // backgroundColorButton          //           this.backgroundColorButton.Location = new System.Drawing.Point(16, 16);          this.backgroundColorButton.Name = "backgroundColorButton";          this.backgroundColorButton.Size = new System.Drawing.Size(264, 32);          this.backgroundColorButton.TabIndex = 0;          this.backgroundColorButton.Text = "Change Background Color";          this.backgroundColorButton.Click += new System.EventHandler(this.backgroundColorButton_Click);          //           // textColorButton          //           this.textColorButton.Location = new System.Drawing.Point(16, 64);          this.textColorButton.Name = "textColorButton";          this.textColorButton.Size = new System.Drawing.Size(264, 32);          this.textColorButton.TabIndex = 1;          this.textColorButton.Text = "Change Text Color";          this.textColorButton.Click += new System.EventHandler(this.textColorButton_Click);          //           // ShowColorsComplex          //           this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);          this.ClientSize = new System.Drawing.Size(292, 109);          this.Controls.AddRange(new System.Windows.Forms.Control[] {                                                                       this.textColorButton,                                                                       this.backgroundColorButton});          this.Name = "ShowColorsComplex";          this.Text = "ShowColorsComplex";          this.ResumeLayout(false);       }       static void Main()        {          Application.Run( new ShowColorsComplex() );       }       private void textColorButton_Click(object sender, System.EventArgs e ){          ColorDialog colorChooser = new ColorDialog();          DialogResult result;          result = colorChooser.ShowDialog();          if ( result == DialogResult.Cancel )             return;                    backgroundColorButton.ForeColor = colorChooser.Color;          textColorButton.ForeColor = colorChooser.Color;       }       private void backgroundColorButton_Click(object sender, System.EventArgs e ){          ColorDialog colorChooser = new ColorDialog();          DialogResult result;          colorChooser.FullOpen = true;          result = colorChooser.ShowDialog();          if ( result == DialogResult.Cancel )             return;          this.BackColor = colorChooser.Color;       }    }