Dialog Boxes (ColorDialog, FontDialog, SaveFileDialog and OpenFileDialog) | BKNMU Junagadh

 


Color Dialog

 

A ColorDialog in C# is like a pop-up window that shows a bunch of colors to choose from. You can use it in your program to let users pick a color easily. When they select a color and close the window, your program gets to know which color they chose. To open this color window, you just need to call the `ShowDialog()` method.

 

In C#, the ColorDialog control lets users pick a color from a range of pre-defined colors, as well as create their own custom colors. When you use a Color Dialog, it usually displays a list of common solid colors for easy selection. Additionally, it provides an option for users to define their own custom colors. This allows users to pick from a wider spectrum beyond just the predefined colors.

 

ColorDialog dlg = new ColorDialog();

dlg.ShowDialog();

 

Example for return selected color name:

 

ColorDialog dlg = new ColorDialog();

dlg.ShowDialog();

if (dlg.ShowDialog() == DialogResult.OK)

{

string str = null;

     str = dlg.Color.Name;

     MessageBox.Show (str);

}

 

 

 

Creating a ColorDialog

 

Creating a ColorDialog control in C# can be done either using a Forms designer during design-time or programmatically using the ColorDialog class at run-time. Unlike other Windows Forms controls, a ColorDialog doesn't have visual properties as its main purpose is to display available colors, allow users to create custom colors, and select a color from them. Once a color is selected, it can be captured and used in the code to apply it to other controls. While it's possible to create a ColorDialog at design-time, it's often simpler and more flexible to create it at run-time programmatically.

 

Design-time

To create a ColorDialog control at design-time in Visual Studio, you can easily do so by dragging and dropping a ColorDialog control from the Toolbox onto a Form. Once added, the ColorDialog appears on the Form and can be configured as needed.

 

Run-time

 

To create a ColorDialog control dynamically at run-time, you start by creating an instance of the ColorDialog class. The following code snippet demonstrates how to create a ColorDialog object:

 

ColorDialog colorDialog = new ColorDialog();

 

This line of code instantiates a new ColorDialog object named `colorDialog`, which can then be further configured and added to the Form's controls as needed.

 

ShowDialog method of ColorDialog displays the ColorDialog. The following code snippet sets background color, foreground color, Text, Name, and Font properties of a ColorDialog.

colorDlg.ShowDialog(); 

 

ColorDialog Properties

 

After placing a ColorDialog control on a Form, the next step is to configure its properties. The easiest way to do this is through the Properties Window. You can open the Properties window by pressing F4 or by right-clicking on the control and selecting the Properties menu item. The Properties window provides a convenient interface for modifying various properties of the ColorDialog control.

 

 

 

 

 

AllowFullOpen

In Figure 1, there's a button labeled "Define Custom Colors" on the ColorDialog. Clicking on this button opens the custom color editor area. Here, users can define colors by setting RGB color values ranging from 0 to 255. Additionally, users can select a color from the color area, as shown in Figure 4. This feature allows users to create custom colors beyond the predefined ones.

 

 

 

Setting the AllowFullOpen property to false on a ColorDialog disables the "Define Custom Color" option, ensuring it is not available. As a result, the ColorDialog will resemble Figure 5, where the custom color option is not visible or accessible to users.

 

 

 

 

 

Example

private void button1_Click(object sender, EventArgs e)

{

    //colorDialog1.ShowDialog();

    if (colorDialog1.ShowDialog() == DialogResult.OK)

    {

        this.BackColor = colorDialog1.Color;

 

    }

}

 

private void button2_Click(object sender, EventArgs e)

{

    MessageBox.Show(colorDialog1.Color.Name);

}

Post a Comment

Thanks for comment.

Previous Post Next Post