Export (0) Print
Expand All
1 out of 5 rated this helpful - Rate this topic

ColorDialog Class

Represents a common dialog box that displays available colors along with controls that allow the user to define custom colors.

For a list of all members of this type, see ColorDialog Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.CommonDialog
            System.Windows.Forms.ColorDialog

[Visual Basic]
Public Class ColorDialog
   Inherits CommonDialog
[C#]
public class ColorDialog : CommonDialog
[C++]
public __gc class ColorDialog : public CommonDialog
[JScript]
public class ColorDialog extends CommonDialog

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The inherited member ShowDialog must be invoked to create this specific common dialog box. HookProc can be overridden to implement specific dialog box hook functionality. Use Color to retrieve the color selected by the user.

When you create an instance of ColorDialog, some of the read/write properties are set to initial values. For a list of these values, see the ColorDialog constructor.

Example

[Visual Basic, C#, C++] The following example illustrates the creation of new ColorDialog. This example assumes that the method is called from within an existing form, that has a TextBox and Button placed on it.

[Visual Basic] 
Protected Sub button1_Click(sender As Object, e As System.EventArgs)
    Dim MyDialog As New ColorDialog()
    ' Keeps the user from selecting a custom color.
    MyDialog.AllowFullOpen = False
    ' Allows the user to get help. (The default is false.)
    MyDialog.ShowHelp = True
    ' Sets the initial color select to the current text color,
    MyDialog.Color = textBox1.ForeColor

    ' Update the text box color if the user clicks OK 
    If (MyDialog.ShowDialog() = DialogResult.OK) Then
        textBox1.ForeColor =  MyDialog.Color
    End If

End Sub 'button1_Click

[C#] 
protected void button1_Click(object sender, System.EventArgs e)
 {
    ColorDialog MyDialog = new ColorDialog();
    // Keeps the user from selecting a custom color.
    MyDialog.AllowFullOpen = false ;
    // Allows the user to get help. (The default is false.)
    MyDialog.ShowHelp = true ;
    // Sets the initial color select to the current text color.
    MyDialog.Color = textBox1.ForeColor ;
    
    // Update the text box color if the user clicks OK 
    if (MyDialog.ShowDialog() == DialogResult.OK)
        textBox1.ForeColor =  MyDialog.Color;
 }
    

[C++] 
protected:
void button1_Click(Object* /*sender*/, System::EventArgs* /*e*/)
 {
    ColorDialog* MyDialog = new ColorDialog();
    // Keeps the user from selecting a custom color.
    MyDialog->AllowFullOpen = false ;
    // Allows the user to get help. (The default is false.)
    MyDialog->ShowHelp = true ;
    // Sets the initial color select to the current text color.
    MyDialog->Color = textBox1->ForeColor ;
    
    // Update the text box color if the user clicks OK 
    if (MyDialog->ShowDialog() == DialogResult::OK)
        textBox1->ForeColor =  MyDialog->Color;
 }
    

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: System.Windows.Forms (in System.Windows.Forms.dll)

See Also

ColorDialog Members | System.Windows.Forms Namespace | CommonDialog

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.