Use Color From Color Dialog Box C Sharp

By | August 28, 2023

This windows forms application will display windows common dialog box for selection of color. The user will select a color. The click event handler for button will set the back ground color of the form according to the color selected by the user.

Using Color Dialog box

Source Code


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void button1_Click(object sender, EventArgs e)
        {

          DialogResult result = colorDialog1.ShowDialog();
          if (result == DialogResult.OK)            
            this.BackColor = colorDialog1.Color;
        }
    }
}

  1. Button Click Event Handler:
    private void button1_Click(object sender, EventArgs e)
    {
    DialogResult result = colorDialog1.ShowDialog();
    if (result == DialogResult.OK)
    this.BackColor = colorDialog1.Color;

    }

    This is the event handler for the button’s click event. When the button is clicked, this method is executed. Here’s what it does:

    • colorDialog1.ShowDialog();: This line displays the built-in dialog box. The user can select a color.
    • this.BackColor = colorDialog1.Color;: After the user selects a color in the dialog box, this line sets the background color of the form (this) to the selected color (colorDialog1.Color).

In C#, a ColorDialog is a common dialog box that allows users to select a color. It’s used to provide a user-friendly way of choosing colors for various purposes within a program, such as changing the background color of a form, customizing text colors, or selecting colors for drawing and painting applications.

Here are some important properties of the ColorDialog class in C#:

  1. Color: This property gets or sets the color that is selected or set in the ColorDialog. When the dialog is shown, if the user selects a color and clicks “OK,” the selected color will be stored in this property.
  2. FullOpen: This property gets or sets a value indicating whether the full set of custom colors is shown in the dialog. If set to true, it displays the full spectrum of colors. If set to false, it shows only the basic color set.
  3. CustomColors: This property gets or sets the array of custom colors that appear in the color dialog box. You can set your own array of colors that will be available for quick selection by the user.
  4. SolidColorOnly: This property gets or sets a value indicating whether the dialog box will restrict users to selecting solid colors only (true) or if they can also select patterns (false).
  5. AllowFullOpen: This property gets or sets a value indicating whether the user can use the dialog to define custom colors. If set to true, the user can select a custom color using the color wheel. If set to false, the custom color option is disabled.
  6. ShowDialog(): This method displays the color dialog box to the user. It returns a DialogResult that indicates whether the user clicked the OK button (DialogResult.OK) or the Cancel button (DialogResult.Cancel).

Loading

Leave a Reply

Your email address will not be published. Required fields are marked *