Changing Background Color in Windows Form dynamically with use of Color Dialog Box

Raj.Trivedi
Posted by in C# category on for Beginner level | Points: 250 | Views : 25759 red flag
Rating: 5 out of 5  
 1 vote(s)

In this article we will see how we can change the background color of windows form dynamically.

Introduction


Just few days back one of my friends told me that he want to change color in windows form dynamically for his post graduation project.We had a nice brainstorming session and then found the solution by checking the control of windows.Let us see the solution.


Objective

  1. Understanding color dialog
  2. Basic Color Picker and Custom Color Picker
  3. How to apply them to background of the form.

Using the code


  1. Create a New Windows Project
  2. Now drag and drop 2 buttons name them as Choose Basic Color and Choose Custom Color respectively.
  3. Now go to the Toolbox -> Dialog Section -> Drag and Drop a Color Dialog box and change the name property to cddialog.

The Windows form should look something like this.




Now go to the code behind write the code on the following button click event.

// Code behind
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ColorSetter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnBasic_Click(object sender, EventArgs e)
        {
            // Opening the color dialog.The full open property is set to false and hence we can open basic color selection
            cddialog.FullOpen = false;
            if (cddialog.ShowDialog() != DialogResult.Cancel)
            {
                this.BackColor = cddialog.Color;
            }
        }

        private void btnCustom_Click(object sender, EventArgs e)
        {   // Opening the color dialog.The full open property is set to true and hence we can open custom color selection
            cddialog.FullOpen = true;
            if (cddialog.ShowDialog() != DialogResult.Cancel)
            {
                this.BackColor = cddialog.Color;
            }
        }
    }
}

Color Dialog :-

Color dialog allows to choose color from basic color setting and custom color setting.

How to Open Basic Color Setting ?

In order to open the basic color setting you have to set the full open property of the colordialog box to false.Once
you set it to false it will open the basic color picker.

How to Custom Basic Color Setting ?

In order to open the basic color setting you have to set the full open property of the colordialog box to true.Once
you set it to true it will open the custom color picker.


Output 1 : Setting Basic Color




Output 2 : Setting Custom Color



Conclusion


This code can be useful for setting colors on forms when needed.

Reference




Page copy protected against web site content infringement by Copyscape

About the Author

Raj.Trivedi
Full Name: Raj Trivedi
Member Level:
Member Status: Member,MVP
Member Since: 6/16/2012 2:04:41 AM
Country: India
Regard's Raj.Trivedi "Sharing is Caring" Please mark as answer if your Query is resolved
http://www.dotnetfunda.com/profile/raj.trivedi.aspx
Raj Trivedi i.e. me started my career as Support Professional and then moved on the Software development eventually reached at these skills Software Development | Enthusiastic Blogger | Content Writer | Technical Writer | Problem Solver | Lecturer on Technology Subjects | Runnerup Award Winner on www.dotnetfunda.com and firm believer in Sharing as a way of Caring Yet this much achieved its still a long way to go and there is biggest dream lying to be one of the best entrepreneurs of India in Technology Department. The Dream has just started and i hope it follows. Highlights are mentioned in details in my profile at http://in.linkedin.com/pub/raj-trivedi/30/61/b30/

Login to vote for this post.

Comments or Responses

Posted by: Shitalr on: 7/17/2013 | Points: 25
I want project in windows form c# for BE final year.
Posted by: Raj.Trivedi on: 7/17/2013 | Points: 25
Hello Shitlar,

What is your ideal for the project

Login to post response

Comment using Facebook(Author doesn't get notification)