ColorDialog. This displays a color selection window. By using the ColorDialog, you can enable a visual interface for selecting a specific color.
Some options are provided through properties on this control. Using ColorDialog is much easier than any custom solution. Here we explore this control.
Example. We will add a ColorDialog to the form and then use it in some C# code. Most of the work you do with the ColorDialog will require the ShowDialog method and also the Color property.
Tip When you invoke ShowDialog, please remember to check the DialogResult. We avoid taking an action when the dialog was canceled.
Here In this code, we show the ColorDialog instance, and then see if the user clicked the OK button to accept.
Finally If OK was clicked, the parent form's background color is set to the color selected in the dialog.
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Show the color dialog.
DialogResult result = colorDialog1.ShowDialog();
// See if user pressed ok.
if (result == DialogResult.OK)
{
// Set form background to the selected color.
this.BackColor = colorDialog1.Color;
}
}
}
}
Properties. The ColorDialog allows you to open a more complete color picker to the right side with a button by default. If AllowFullOpen is set to False, this button is disabled.
And If FullOpen is set to True, the full color picker is shown automatically when the ColorDialog is opened.
CustomColors. You can acquire the custom colors in the ColorDialog that were added by the user. You can also add your own colors there with code.
Info This is an integer array—you can convert those integers into Color types if necessary.
Summary. ColorDialog provides the necessary functionality of a visual color picker. There are only a few useful properties on this control, such as the Color property.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 24, 2024 (simplify).